1

I am new to PHP Script , I am trying to iterate through foreach loop in PHP I am getting only first value result loop is not going to next element in array.

// Parsing data
$Id = htmlentities($_POST["Id"]);
$tables  = array("Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday") ; 

foreach ($tables as $value) {       
  //Call CheckData function
  $result = checkData($value ,$Id , $conn);

if(!empty($result)) {

    $returnArray["status"] = true  ;
    $returnArray["message"] = $result ;
     $returnArray["Day of Week"] = $value  ;     
    echo json_encode($returnArray) ;       
} else {
    $returnArray["status"] = false ;
    $returnArray["message"] = "No data found for ' $value'";
    $returnArray["Day of Week"] = $value  ;  
      echo json_encode($returnArray) ;
}
}
    return ;

I get result for Monday only not for Tuesday and so on.

//Code for CheckData

function checkData($value ,$Id , $conn) {

   $returnValue[] = array() ;
    //SQL query
    $sql = "SELECT * FROM $value  WHERE Id = '$Id'" ;

    //Store the result in $result

     $result = mysqli_query($conn , $sql);

      while ($row = mysqli_fetch_array($result)) {

        $returnValue[]  = array_map(utf8_encode ,$row);

    }
    return $returnValue ;

}
swift_USer
  • 149
  • 3
  • 12
  • @Ivar This Php script is for iOS in front end. – swift_USer Jun 19 '17 at 08:12
  • Make a multidimensional array with name of days and pass all the array element to array. so if first is Monday then create an array of monday with key element and then Tuesday and so on at last after complete loop return json response. remove json function before the loop – Pankaj Makwana Jun 19 '17 at 08:12
  • Building a return array, but not returning it?!? WHat purpose does this serve? – Mark Baker Jun 19 '17 at 08:12
  • please provide the code of checkData() function – Sehdev Jun 19 '17 at 08:14
  • Have you turned error logging on? If so is there any error? It wouldn't surprise me if there is an error which terminates the rest of your script. – Ivar Jun 19 '17 at 08:16
  • @Ivar I don't get any error. I just get result for Monday only – swift_USer Jun 19 '17 at 08:18
  • do you have table named with days of week? and can you check the capitalization of the tables – Exprator Jun 19 '17 at 08:21
  • @Spurti Take a look at [this question](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). It may throw an error, but you just don't see it. I don't see anything wrong with your code. (Which would result in an error, you still should use prepared statements and the fact that you have 1 table for every day of the week seems like a bad database structure.) – Ivar Jun 19 '17 at 08:22
  • You created tables for Monday, Tuesday ... ? – hungrykoala Jun 19 '17 at 08:23
  • Yes I have tables for Monday till Sunday because each table has different operations to do. So I have to create different Tables – swift_USer Jun 19 '17 at 08:25
  • Would you be able to provide the code for checkData? as this is the only place i can see this going wrong and not returning the correct indexes for the result array. – jwtea Jun 19 '17 at 08:14

1 Answers1

0
//Parsing data
$Id = htmlentities($_POST["Id"]);
$tables  = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") ;
$returnArray = [];
$intCount = 0;
foreach ($tables as $value) {       
    //Call CheckData function
    $result = checkData($value ,$Id , $conn);
    if (!empty($result)) {
        $returnArray[$intCount]["status"] = true  ;
        $returnArray[$intCount]["message"] = $result ;
        $returnArray[$intCount]["Day of Week"] = $value  ;     
        echo json_encode($returnArray) ;       
    } else {
       $returnArray[$intCount]["status"] = false ;
       $returnArray[$intCount]["message"] = "No data found for ' $value'";
       $returnArray[$intCount]["Day of Week"] = $value  ;  
       echo json_encode($returnArray) ;
    }
    $intCount++;
}
return ;