[英]How to span table dynamically (colspan) using PHP?
我有这样的预订时段的房间:
07.00,07.55,08.50,09.45,10.40,11.35,12.30,13.00,13.55,14.50,15.45,16.40,17.35,18.30.
这是一个预订时间示例:
预订时间A1室:
==> 1 : 07.00 - 09.45 ~ credit 3
==> 2 : 10.40 - 12.30 ~ credit 3
==> 3 : 13.00 - 15.45 ~ credit 3
注意:1个学分-> 55分钟
现在我有一个数组:
阵列预定时间:
$startBook[0] = 07.00,
$startBook[1] = 10.40,
$startBook[2] = 13.00,
$endBook[0] = 09.45,
$endBook[1] = 12.30,
$endBook[2] = 15.45
我想像这样显示表格:
我如何动态设置colspan取决于功劳。 积分不总是3,也可以是2或4。
这是html:
<table>
<tr>
<td>Room</td>
<td colspan="" >07.00</td>
<td colspan="" >07.55</td>
so on...
</tr>
<tr>
<td>A1</td>
<td colspan="..."></td>
<td>09.45</td>
</tr>
</table>
您必须检查每个开始的预订时间。
<table border="1">
<tr>
<th>Room</th>
<?php
$rooms = array("a", "b", "c", "d", "e", "f");
$hours = array('07.00','07.55','08.50','09.45','10.40','11.35','12.30','13.00','13.55','14.50','15.45','16.40','17.35','18.30');
$startBook = array('07.00', '10.40', '13.00');
$endBook = array('09.45', '12.30', '15.45');
// Header
foreach ($hours as $hourIndex => $hour) {
echo "<th>$hour</th>";
}
echo "\n</tr>";
foreach ($rooms as $room) {
echo "<tr>
<td>" .$room['name'] ."</td>";
$indexCurrentBook = -1;
$tdColspan = 0;
// Cicle every hour
foreach ($hours as $hourIndex => $hour) {
// Checking if there is an open book
if ($indexCurrentBook >= 0) {
if ($hour == $endBook[$indexCurrentBook]) { // Checking for the current book as ended
echo "<td colspan=\"$tdColspan\" class=\"booked\">booked</td>";
$indexCurrentBook = -1;
} else {
$tdColspan++;
}
}
if ($indexCurrentBook < 0) {
// No open book, searching for a new one
$tdColspan = 1;
foreach($startBook as $startBookIndex => $startBookDate) {
if ($hour == $startBookDate) {
$indexCurrentBook = $startBookIndex;
}
}
// If nothing as found, I write a blank cell
if ($indexCurrentBook < 0) echo "<td class=\"blank\">blank</td>";
}
}
echo "</tr>";
} ?>
</table>
bat,感谢您的回答,我尝试使用这样的数组集执行代码:
$array = array("a", "b", "c", "d", "e", "f");
$hours=array('07.00','07.55','08.50','09.45','10.40','11.35','12.30','13.00','13.55','14.50','15.45','16.40','17.35','18.30');
$startBook = array('07.00', '10.40', '13.00');
$endBook = array('09.45', '12.30', '15.45');
但它显示的是这样的表(所有房间都已预订):
我如何显示时间...
*如果未显示图像,则为显示的表格:
a | booked
b | booked
c | booked
d | booked
e | booked
f | booked
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.