dounai9294 2014-05-05 10:28
浏览 69
已采纳

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

I have slot times booking room like this :

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.

This is a booking time example :

Book Time Room A1 :

      ==> 1 : 07.00 - 09.45 ~ credit 3
      ==> 2 : 10.40 - 12.30 ~ credit 3
      ==> 3 : 13.00 - 15.45 ~ credit 3

note : 1 credit --> 55 minutes

And now I have an array :

Array Book Time :

$startBook[0] = 07.00, 
$startBook[1] = 10.40, 
$startBook[2] = 13.00, 
$endBook[0] = 09.45, 
$endBook[1] = 12.30, 
$endBook[2] = 15.45

I want to display the table like : table

How I can set the colspan dynamically depend on the credit. The credit not always 3, it also can be 2 or 4.

This is the 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条回答 默认 最新

  • dpi9530 2014-05-05 10:59
    关注

    You have to check every starting booking hour.

    <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 "
    </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条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿