我正在使用mysql在php页面上完成学校的时间表,我有像这样的mysql表
klass | day | lesson_name
7A | Monday | lesson1
7A | Monday | lesson5
7A | Monday | lesson3
7A | Tuesday | lesson2
7A | Tuesday | lesson1
7A | Tuesday | lesson4
7A | Wednesday| lesson5
7A | Wednesday| lesson2
7A | Wednesday| lesson1
7A | Thursday | lesson3
7A | Thursday | lesson5
7A | Thursday | lesson4
7А | Friday | lesson2
7А | Friday | lesson3
7А | Friday | lesson5
...And other classes
这是我的代码的一部分:
<table border="1">
<tr align=center>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
</tr>
<?php
//$con = mysqli_connect("localhost", "user", "password", "database");
//if (mysqli_connect_errno()) {
// echo "Failed to connect to MySQL: " . mysqli_connect_error();
//}
$klass = $_POST['klass'];
$res1 = mysqli_query($con, "SELECT * FROM `Timetable` WHERE `klass` LIKE '$klass' AND `day` LIKE 'Monday'");
$res2 = mysqli_query($con, "SELECT * FROM `Timetable` WHERE `klass` LIKE '$klass' AND `day` LIKE 'Tuesday'");
$res3 = mysqli_query($con, "SELECT * FROM `Timetable` WHERE `klass` LIKE '$klass' AND `day` LIKE 'Wednesday'");
$res4 = mysqli_query($con, "SELECT * FROM `Timetable` WHERE `klass` LIKE '$klass' AND `day` LIKE 'Thursday'");
$res5 = mysqli_query($con, "SELECT * FROM `Timetable` WHERE `klass` LIKE '$klass' AND `day` LIKE 'Friday'");
foreach ($res5 as $row5) {
echo "<tr>";
echo "<td align='center'>$row1[lesson_name]</td>";
echo "<td align='center'>$row2[lesson_name]</td>";
echo "<td align='center'>$row3[lesson_name]</td>";
echo "<td align='center'>$row4[lesson_name]</td>";
echo "<td align='center'>$row5[lesson_name]</td>";
echo "</tr>";
}
?>
</table>
使用该代码我只显示星期五列。我知道问题在于foreach。我尝试将其重写为:foreach($ res1为$ row1,$ res2为$ row2,$ res3为$ row3,$ res4为$ row4,$ res5为$ row5),但后来没有显示任何内容;(
如何重写我的foreach并正确显示我的时间表中的所有专栏和课程?
UPD。我已经像Philipp说的那样做了,但这段代码没有显示任何内容:
<table border="1">
<tr align=center>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
</tr>
<?php
//$con = mysqli_connect("localhost", "user", "password", "database");
//if (mysqli_connect_errno()) {
// echo "Failed to connect to MySQL: " . mysqli_connect_error();
//}
$klass = $_POST['klass'];
$res = mysqli_query($con, "SELECT * FROM `Timetable` WHERE `klass` LIKE '$klass'");
$groupedLessons = array_reduce($res, function($groups, $lesson) {
if (!array_key_exists($lesson['day'], $groups)) {
$groups[$lesson['day']] = [];
}
$groups[$lesson['day']][] = $lesson;
return $groups;
}, []);
foreach($groupedLessons as $day => $lessons) {
foreach ($lessons as $lesson) {
<tr>
<td><?= $klass ?></td>
<td><?= $day ?></td>
<td><?= $lesson['lesson_name'] ?></td>
</tr>
}
}
?>
</table>
答案 0 :(得分:1)
最好的方法是只进行一次数据库查询,即可立即获取所有课程:
SELECT * FROM `Timetable` WHERE `klass` LIKE '$klass'
然后,您可以根据工作日对值进行分组:
$groupedLessons = array_reduce($allLessons, function($groups, $lesson) {
if (!array_key_exists($lesson['day'], $groups)) {
$groups[$lesson['day']] = [];
}
$groups[$lesson['day']][] = $lesson;
return $groups;
}, []);
现在,您可以使用两个嵌套的foreach循环遍历数据:
foreach($groupedLessons as $day => $lessons) {
foreach ($lessons as $lesson) {
?>
<tr>
<td><?= $klass ?></td>
<td><?= $day ?></td>
<td><?= $lesson['lesson_name'] ?></td>
</tr>
<?php
}
}