如何使用PHP动态跨表(colspan)?

时间:2014-05-05 10:28:05

标签: 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

我想显示如下表:table

如何动态设置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>

2 个答案:

答案 0 :(得分:1)

您必须检查每个开始预订时间。

<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>

答案 1 :(得分:0)

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');

但它会像这样显示表格(所有房间都已预订):enter image description here

我如何显示小时......

*如果图像未显示,则显示的是表格:

a | booked
b | booked
c | booked
d | booked
e | booked
f | booked