简体   繁体   English

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

[英]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 班次表具有: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>";

} }

?> ?>

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. 注意:请记住,必须更改此部分$x <= 10以适合实际的数组大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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