如何使用PHP从SQL数据填充表

时间:2016-04-10 19:52:59

标签: php mysql

我有一个带标题的表格;周日,周一,周二,周三,周四,周五和周六。我收集了用户"轮班"来自数据库...

<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>"

因此,每天可以有任意数量的轮班,但取决于轮班的当天,我需要将班次置于正确的表格标题下。

班次表有:shift_id,姓名,日期,日

$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>";

}

&GT;

0 个答案:

没有答案