Colspan打破桌子

时间:2016-09-12 17:04:30

标签: php foreach html-table

ORIGINAL

我有一个页面显示某一天的所有公司董事会会议室和预订。它看起来像这样:

enter image description here

如您所见,某些行会创建额外的<td>

以下是生成表格的代码:

<table celladding="0" cellspacing="0" class="table table-bordered col-xs-12">
    <thead>
        <tr style="background-color:#FFF">
            <?php
            $width = (100 / (count($boardrooms->toArray()) + 1));
            $width = 100 / 7;
            ?>
            <th style="border-right:none; width:<?= $width; ?>%; border-bottom: none;">Boardroom</th>
            <?php
            foreach($boardrooms as $boardroom) { ?>
                <th rowspan="2" style="width:<?= $width; ?>%"><?php echo $boardroom ?></th>
            <?php } ?>
        </tr>
        <tr>
            <th>Time</th>
        </tr>
    </thead>
    <?php
    $writerow = false;
    for($hour = 8; $hour < 17; $hour++) {
        for($minute = 0; $minute < 4; $minute++) {
            echo '<tr><td>' . $hour . ':' . str_pad(($minute*15),2,"0") . '</td>';
            foreach($boardrooms as $boardroom) {
                foreach($boardroomBookings as $booking) {
                    if(date('H:i',strtotime($hour . ':' . str_pad(($minute*15),2,"0"))) >= date('H:i',strtotime($booking->start_time)) &&
                        date('H:i',strtotime($hour . ':' . str_pad(($minute*15),2,"0"))) < date('H:i',strtotime($booking->end_time)) &&
                        $boardroom == $booking->boardroom->name && (empty($boardroomWritten[$booking->id]) || !$boardroomWritten[$booking->id])) {
                        $boardroomWritten[$booking->id] = true;
                        $writerow = true;
                        $rowspan = (strtotime($booking->end_time) - strtotime($booking->start_time))/900;
                        echo '<td style="background-color:#8cc63f; border-bottom:none" rowspan="' . $rowspan . '">' . $this->Html->link($booking->name, array('action' => 'view', $booking->id)) . '</td>';
                        break;
                    } else {
                        $writerow = false;
                    }
                }
                if(!$writerow) {
                    echo '<td></td>';
                }
            }
            echo '</tr>';
        }
    }
    ?>
</table>

如何阻止行在有预订的行上显示额外的空单元格?

修改

这是与黑色边框相同的表格:

enter image description here

没有预订就是这样的:

enter image description here

预订应该是什么样子:

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您没有预订,则需要插入一对td,除非您在colspan内:

    <?php
for($hour = 8; $hour < 17; $hour++) {
    for($minute = 0; $minute < 4; $minute++) {
        $bookTime = date('H:i ',strtotime($hour . ':' . str_pad(($minute*15),2,"0"))) ;
        echo '<tr><td>' . $bookTime . '</td>';
        foreach($boardrooms as $boardroom) {
            $hasBooking = false;
            foreach($boardroomBookings as $booking) {
                $sTime = strtotime($booking->start_time) ;
                $eTime = strtotime($booking->end_time) ;
                $startTime = date('H:i',$sTime);
                $endTime = date('H:i', $eTime) ; 
                if($bookTime >=  $startTime  && $bookTime < $endTime && $boardroom == $booking->boardroom->name) {
                    $hasBooking = true;
                    if(empty($boardroomWritten[$booking->id]) || !$boardroomWritten[$booking->id]) {
                        $boardroomWritten[$booking->id] = true;
                        $rowspan = ($eTime - $sTime)/900  ;
                        echo '<td style="background-color:#8cc63f; border:1px black solid" rowspan="' . $rowspan . '">' . "somelink" . '</td>';
                        break;
                    }                        
                }
            }
            if (!$hasBooking) {
                echo "<td></td>" ;
            }
        }
        echo '</tr>';
    }
}