简体   繁体   中英

How to populate table from SQL data using PHP

I have a table with headers; Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday. I have gathered user "shifts" from a database...

<tr>
      <th>Sunday</th>
      <th>Monday</th>
      <th>Tuesday</th>
      <th>Wednesday</th>
      <th>Thursday</th>
      <th>Friday</th>
      <th>Saturday</th>
    </tr>
<?php $sql4 = "SELECT * FROM shifts WHERE date = '" . $sunday . "' or date = '" . $monday . "' 
               or date = '" . $tuesday . "' or date = '" . $wednesday . "' 
               or date = '" . $thursday . "' or date = '" . $friday . "' 
               or date = '" . $saturday . "'"; 

      $result1 = $database->query($sql4);

      echo"<tr>";
      while ($row4 = mysqli_fetch_array($result1)){ 
             echo "<td>";
             echo $row4['name'];
             echo "</td>";
      }
      echo "</tr>"

So there can be any amount of shifts per day, but dependant on what day the shift is on i need to place the shift under the correct table heading.

The shifts table has: shift_id, name, date, day

$resultsArray = array();
while ($row4 = mysqli_fetch_array($result1)){
     $weekDay = $row4['day'];
     $resultsArray[$weekDay][] = $row4['name'];
 }

for ($x = 0; $x <= count($resultsArray); $x++) {

 $sundayData = (!empty($resultsArray['Sunday'][$x]) ) ? $resultsArray['Sunday'][$x] : "";
 $mondayData = (!empty($resultsArray['Monday'][$x]) ) ? $resultsArray['Monday'][$x] : "";
 $tuesdayData = (!empty($resultsArray['Tuesday'][$x]) ) ? $resultsArray['Tuesday'][$x] : "";
 $wednesdayData = (!empty($resultsArray['Wednesday'][$x]) ) ? $resultsArray['Wednesday'][$x] : "";
 $thursdayData = (!empty($resultsArray['Thursday'][$x]) ) ? $resultsArray['Thursday'][$x] : "";
 $fridayData = (!empty($resultsArray['Friday'][$x]) ) ? $resultsArray['Friday'][$x] : "";
 $saturdayData = (!empty($resultsArray['Saturday'][$x]) ) ? $resultsArray['Saturday'][$x] : "";


 echo "<tr>";
 echo "<td>".$sundayData."</td>";
 echo "<td>".$mondayData."</td>";
 echo "<td>".$tuesdayData."</td>";
 echo "<td>".$wednesdayData."</td>";
 echo "<td>".$thursdayData."</td>";
 echo "<td>".$fridayData."</td>";
 echo "<td>".$saturdayData."</td>";
 echo "</tr>";

}

?>

It would be easier to answer your question if I could see the exact structure of your data, but I'll give this a whirl anyway and hopefully you can see where i'm going with it.

What I would do is create an array and fill in the information as you go through the results. Once you have the new resulting array, build your table.

  $resultsArray = array();
  while ($row4 = mysqli_fetch_array($result1)){
         $weekDay = $row4['day'];
         $resultsArray[$weekDay][] = $row4['shift'];
  }

Now you have an array that contains all the shifts organized by day. Looping through this array will allow you to create your table the way you want.

This is untested so may have some mistakes in it, but here is an example...

for ($x = 0; $x <= 10; $x++) {
     echo "<tr>";
     echo "<td>".$resultsArray['Sunday'][$x]."</td>";
     echo "<td>".$resultsArray['Monday'][$x]."</td>";
     echo "<td>".$resultsArray['Tuesday'][$x]."</td>";
     echo "<td>".$resultsArray['Wednesday'][$x]."</td>";
     echo "<td>".$resultsArray['Thursday'][$x]."</td>";
     echo "<td>".$resultsArray['Friday'][$x]."</td>";
     echo "<td>".$resultsArray['Saturday'][$x]."</td>";
     echo "</tr>";
}

Note : Keep in mind you'll have to change this portion $x <= 10 to fit your actual array size.

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