<table class="table table-hover">
<thead>
<tr>
<th scope="col">Monday</th>
<th scope="col">Tuesday</th>
<th scope="col">Wednesday</th>
<th scope="col">Thursday</th>
<th scope="col">Friday</th>
<th scope="col">Saturday</th>
<th scope="col">Sunday</th>
</tr>
</thead>
<tbody>
<?php
if(isset($_GET['class_id'])) {
$class_id = $_GET['class_id'];
} else {
$class_id = '1'; //default class id => Yoga
}
$mQuery = "SELECT * FROM timetable where class_id = '".$class_id."'"; //code to select from database
$result = $pdo-> query($mQuery);
while($row = $result->fetch(PDO::FETCH_ASSOC)):
$timeslot = $row['timeslot'];
// pass delimiter and string to explode function
$timeslot_ar = explode(',', $timeslot);
?>
//code to display result in table
<tr class="table-active">
<?php if(date('l', strtotime($row['date'])) == 'Monday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Tuesday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Wednesday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Thursday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Friday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Saturday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Sunday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
</tr>
<?php endwhile; ?>
</tbody>
</table>
我一直试图让结果整天显示在一行中,然后在已经存在相同名称的某天时创建新行。我一直在试图找出背后的登录信息,经过无数堆栈溢出答案和YouTube教程后,我到了这一步。我仍然无法将它们放在同一行中,如果有人愿意解释这背后的方法,我将不胜感激。
最佳答案
将<tr></tr>
放在while()
循环之外:
<tr class="table-active">
<?php
// other code
while($row = $result->fetch(PDO::FETCH_ASSOC)):
//code for table cells
?>
</tr>