简体   繁体   中英

Iterate through foreach in Php script

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 ;

}
//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 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM