Jump to content

Recommended Posts

I have an application which allows users to make a booking for a room. user picks the date, start time and end time.

 

What I want to do is create a colour coded 'table of availability'.

 

Basically draw a table, showing 'blocks' of 1 hour intervals, if that hour has been booked it is unavailable, color red, otherwise color green.

 

The bookings are stored in the database table 'bookings'

'booking_id |  date          | start_time | end_time | user_id | room_id |
-------------------------------------------------------------------
1             | 2009-09-30 | 16:00:00   | 17:00:00 |    1      |  1        |
2             | 2009-09-30 | 12:00:00   | 14:00:00 |    1      |  1        |

 

I'm pulling the data from the database for that room and date into an array

 

SELECT * 
FROM bookings
WHERE date= 2009-09-30

 

would give an array:

array( '0' => array('booking_id'           => '1', 
                          'date'              => '2009-09-30',
                          'start_time'       => '16:00:00', 
                          'end_time'         => '17:00:00',
                          'user_id'            => '1', 
                          'room_id            => '1', 
                        );
          '1' => array('booking_id'           => '2', 
                          'date'              => '2009-09-30',
                          'start_time'       => '12:00:00', 
                          'end_time'         => '14:00:00',
                          'user_id'            => '1', 
                          'room_id            => '1', 
                        );
       ) ;

 

what i need to do is write out a table where each cell represents an hour in time and color cells in which represent hours between start_time, and end_time in the array using a loop.

 

I just can't get my head around the loop required.

 

Any help would be greatly appreciated.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/176078-solved-availability-table/
Share on other sites

this is quite interesting :)

 

try making the query like this:

 

SELECT *

FROM bookings

WHERE date= 2009-09-30

ORDER BY room_id ASC, start_time ASC

 

to keep all the rooms in order than all the start_times in order

 

then loop through the results with something like this

<?php

$prev_room = -1;

$hours = array();

while ($r = mysql_fetch_assoc($theQuery)) {

if ($r['room_id'] != $prev_room) { $hours[$r['room_id']] = array_combine($k = range(0,23),array_fill(0,23,'available')); $prev_room = $r['room_id']; }

$e = explode(':',$r['start_time'].':'.$r['end_time']);

$s = $e[0];

$e = $e[3];

while ($e > $s) {

$hours[$r['room_id']][$e - 1] = 'not available';

$e++;

}

}

print_r($hours);

?>

then $hours should hold an array of available and not-available :)

sorry don't use the above.. I made a boo-boo :)

 

<?php
$prev_room = -1;
$hours = array();
while ($r = mysql_fetch_assoc($theQuery)) {
if ($r['room_id'] != $prev_room) { $hours[$r['room_id']] = array_combine(range(0,23),array_fill(0,23,'available')); $prev_room = $r['room_id']; }
$e = explode(':',$r['start_time'].':'.$r['end_time']);
$s = $e[0];
$e = $e[3];
while ($e > $s) {
	$hours[$r['room_id']][$e - 1] = 'not available';
	$s++;
}
}
print_r($hours);
?>

 

use that instead :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.