使用foreach在html表中回显mysql数据

我正在根据日期从数据库中检索记录,并希望根据日期显示所有记录。例如,星期一的记录应该低于星期一的标题,而星期二的记录应该低于星期二的标题。就我而言,有时星期一的一些记录在星期三以下可见。我如何实现这一目标?


<th>Monday</th>

<th>Tuesday</th>

<th>Wednesday</th>


<?php foreach($result as $r){

?>

<tr>

<?=$r->colname?>

</tr>


<?php }?>


阿晨1998
浏览 238回答 1
1回答

qq_遁去的一_1

你可以这样做:$query="SELECT * FROM table WHERE stuff = "otherstuff"";&nbsp; &nbsp;&nbsp;$res=$conn->query($query);&nbsp; &nbsp; &nbsp;while($row=$res->fetch_assoc()){&nbsp; &nbsp; &nbsp; &nbsp; echo "<table>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<tr><th>".$row['date']."</th></tr>"; //here you get each date filtered by rows&nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<td>".$row['stuff']."</td>"; //data1&nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<td>".$row['otherstuff']."</td>"; //data2&nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; echo "</table>";}在 html5 表中打印 myslq 查询的内容时,我建议使用 while() 而不是 foreach()。它遍历 while() 并为每一行生成一个表。您的表格应如下所示:Date&nbsp; &nbsp; Stuff&nbsp; Otherstuff&nbsp; MorestuffMonday&nbsp; data1&nbsp; data2&nbsp; &nbsp; &nbsp; &nbsp;data3&nbsp; &nbsp; &nbsp; &nbsp; //this one gets an own tableTuesday XY&nbsp; &nbsp; &nbsp;XY&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XY&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//this one gets the next table
打开App,查看更多内容
随时随地看视频慕课网APP