问题绘制带有填充槽的时间表

时间:2013-05-16 07:02:12

标签: php cakephp-2.0

我有一个董事会预订系统,一个人预订董事会会议室,网上可以看到公司员工的预订。目前,如果当天有一个预订,它会正确绘制。

有几个会议室,其中任何一个都可以在8:00到17:00之间随时预订。

如果有一个会议室预订,它看起来像这样:

enter image description here

但是当同一天有另一个会议室预订时,它看起来像这样:

enter image description here

我创建网格的for循环如下所示:

for($h = 8; $h < 17; $h++) { // hours
    for($m = 0; $m < 4; $m++) { // 15 minutes
        echo '<tr><td>' . $h . ':' . str_pad(($m*15),2,"0") . '</td>'; //plots the timeslot
        foreach($boardrooms as $boardroom) { // array of boardrooms
            foreach($boardroomBookings as $booking) { // array of boardroom bookings
                if(date('H:i',strtotime($h . ':' . str_pad(($m*15),2,"0"))) >= date('H:i',strtotime($booking['BoardroomBooking']['start_time'])) &&
                date('H:i',strtotime($h . ':' . str_pad(($m*15),2,"0"))) < date('H:i',strtotime($booking['BoardroomBooking']['end_time'])) &&
                $boardroom['Boardroom']['name'] == $booking['Boardroom']['name']) {
                    echo '<td rowspan="0" style="background-color:#8cc63f; border-bottom:none">' . $this->Html->link($booking['BoardroomBooking']['name'], array('action' => 'view', $booking['BoardroomBooking']['id'])) . '</td>'; // writes the name into the timeslot
                } else {
                    echo '<td></td>'; // writes an empty block
                }
            }
        }
        echo '</tr>'; // closes the row
    }
}

我做错了什么?

更新

这是淡化数组(仅显示使用的数据);

$boardrooms = array(
    (int) 0 => array(
        'Boardroom' => array(
            'name' => 'Big Boardroom (Pretoria)'
        )
    ),
    (int) 1 => array(
        'Boardroom' => array(
            'name' => 'Small Boardroom (Pretoria)'
        )
    ),
    (int) 2 => array(
        'Boardroom' => array(
            'name' => 'Medium Boardroom (No Screen) (Pretoria)'
        )
    ),
    (int) 3 => array(
        'Boardroom' => array(
            'name' => 'Big Boardroom (Floor 3) (Pretoria)'
        )
    ),
    (int) 4 => array(
        'Boardroom' => array(
            'name' => 'Big Boardroom (Durban)'
        )
    ),
    (int) 5 => array(
        'Boardroom' => array(
            'name' => 'Big Boardroom (Cape Town)'
        )
    )
)

$boardroomBookings = array(
    (int) 0 => array(
        'BoardroomBooking' => array(
            'id' => '1206',
            'name' => 'asdfasdf',
            'boardroom_id' => '1',
            'date' => '2013-05-21',
            'start_time' => '08:15:00',
            'end_time' => '10:00:00'
        ),
        'Boardroom' => array(
            'id' => '1',
            'name' => 'Big Boardroom (Pretoria)'
        )
    ),
    (int) 1 => array(
        'BoardroomBooking' => array(
            'id' => '1208',
            'name' => 'Test 2',
            'boardroom_id' => '4',
            'date' => '2013-05-21',
            'start_time' => '09:00:00',
            'end_time' => '12:00:00'
        ),
        'Boardroom' => array(
            'id' => '4',
            'name' => 'Big Boardroom (Floor 3) (Pretoria)'
        )
    )
)

1 个答案:

答案 0 :(得分:1)

所以我修好了。

我认为这可能是因为$ boardroomBookings循环以及预订的会议室和循环会议室之间的比较,所以我使用了一些很好的调整来使其工作。

for($h = 8; $h < 17; $h++) {
    for($m = 0; $m < 4; $m++) {
        echo '<tr><td>' . $h . ':' . str_pad(($m*15),2,"0") . '</td>';
        foreach($boardrooms as $boardroom) {
            foreach($boardroomBookings as $booking) {
                if(date('H:i',strtotime($h . ':' . str_pad(($m*15),2,"0"))) >= date('H:i',strtotime($booking['BoardroomBooking']['start_time'])) &&
                date('H:i',strtotime($h . ':' . str_pad(($m*15),2,"0"))) < date('H:i',strtotime($booking['BoardroomBooking']['end_time'])) &&
                $boardroom['Boardroom']['name'] == $booking['Boardroom']['name']) {
                    $writerow = true;
                    $rowspan = (strtotime($booking['BoardroomBooking']['end_time']) - strtotime($booking['BoardroomBooking']['start_time']))/900;
                    echo '<td style="background-color:#8cc63f; border-bottom:none">' . $this->Html->link($booking['BoardroomBooking']['name'], array('action' => 'view', $booking['BoardroomBooking']['id'])) . '</td>';
                    break;
                } else {
                    $writerow = false;
                }
            }
            if(!$writerow) {
                echo '<td></td>';
            }
        }
        echo '</tr>';
    }
}