After a little help here. I'm looking to produce some code that will take an incoming array of data, and order the data by time (This I can do easily enough)
There is 22 blocks of time in my table so I need each row to equate to 22 (Each item in the array has a block value) this I can do
What I am after is taking that array of data and say if a piece of data is 3 slots from 09:00am it takes 3 slots (I can do) but if 09:00am is the first item for that day i need the loop to first create 1 block slots until it finds the first slot in the array, then if the next slot set is at 14:00 it fills in the slots in between
i.e. Instead of
test1, 2slots, 09:00, room1
test1, 1slot, 12:00, room1
test1, 2slots, 13:00, room1
test1, 4slots, 15:00, room1
10
11
12
13
...
22
I would like
1
2
3
4
test1, 2slots, 09:00, room1, 6
7
8
9
10
test1, 1slot, 12:00, room1, 11
12
test1, 2slots, 13:00, room1, 14
15
16
test1, 4slots, 15:00, room1, 20
21
22
my function is currently (This returns the first block)
if(is_array($meetrooms)){
$i = 0;
foreach($meetrooms as $valuer){
$b = $valuer['num_blocks'];
$i += $b;
?>
<span><?php echo $valuer['title'] . " " . $valuer['num_blocks'] . " " . $valuer['room_name'] . " " . $i . " " . br() ;?></span>
<?php
}
if($i != 22){
while($i <= 22){ ?>
<span><?php echo $i . br() ;?></span>
<?php
$i++;
}
}
}
My array is reduced but structured as
[0] => Array
(
[mr_id] => 3
[title] => test1 meeting
[description] => Some meeting going on here
[staff_id] => 2454
[room_name] => room1
[start_date] => 2012-04-20 00:43:58
[start_time] => 08:00:00
[end_date] => 2012-04-20 00:43:58
[end_time] => 09:00:00
[num_blocks] => 2
)
Any help would be fantastic!!! Need anything else please ask
EDIT:
I'm 90% the way there now
Only problem is It's concatenating the value outside the while loop to the while loop . . . ???
if(is_array($meetingrooms))
{
$time = date('H:i', strtotime("07:00"));
$i = 1;
foreach($meetingrooms as $valuer)
{
while ($time < $valuer['start_time'])
{ ?>
<span><?php echo "....." . $time . " " . $i . ".....";?></span>
<?php
$prev = date('H:i', strtotime($time));
$next = strtotime('+30mins', strtotime($prev));
$time = date('H:i', $next);
$i++;
}
$nummins = $valuer['num_blocks'] * 30 - 30;
$next = strtotime('+' . $nummins . 'mins', strtotime($time));
$time = date('H:i', $next);
$b = $valuer['num_blocks'];
echo "td" . $valuer['num_blocks'];
$i = $i + 1;
}
$timetofinish = date('H:i', strtotime("18:00"));
if ($time < $timetofinish)
{
while ($time < $timetofinish)
{
echo "<span>......$time..." . $i . "</span>";
$prev = date('H:i', strtotime($time));
$next = strtotime('+30mins', strtotime($prev));
$time = date('H:i', $next);
$i++;
}
}
}
What it now returns is
.....07:00 1..... .....07:30 2..... .....08:00 3..... td2 .....09:00 5..... .....09:30 6..... .....10:00 7..... .....10:30 8..... td1 .....11:00 10..... .....11:30 11..... .....12:00 12..... td4 .....14:00 14..... .....14:30 15..... .....15:00 16..... td1......15:30...18......16:00...19......16:30...20......17:00...21......17:30...22
Are you trying to create some sort of a classroom reservation time-table?
date_default_timezone_set ( 'GMT' );
$meetingrooms = array ( /* ... YOUR DATA ... */ );
define ('MAX_SLOTS', 22);
define ('START_TIME', strtotime('05:00'));
define ('END_TIME', strtotime('22:00'));
$rooms = array ( "room1", "room2" );
$reserved = array();
foreach ($rooms as $room) {
$reserved[$room] = array();
for($t = START_TIME; $t <= END_TIME; $t = strtotime('+30mins', $t)) {
$reserved[$room][$t] = "FREE";
}
}
foreach ($meetingrooms as $valuer) {
$t = strtotime($valuer['start_time']);
while ( $t < strtotime($valuer['end_time'])) {
$reserved[$valuer['room_name']][$t] = $valuer;
$t += strtotime('+30mins', $t);
}
}
foreach ($reserved as $roomname=>$reservations) {
echo "{$roomname}: ";
foreach ($reservations as $t=>$valuer) {
echo date("H:i", $t).( ($valuer === "FREE") ? "...." : $valuer['title'])." | ";
}
echo "
";
}
Now you can modify the output (add HTML/XML/whatever) to suit your requirements.
Check this sample code.
<?php
//Sample time loop.
$time = new DateTime('2011-11-17 08:00:00');
$end = new DateTime('2011-11-17 20:00:00');
while ($time < $end) {
echo $stamp = $time->format('h:i a') . '</br>';
$time->add(new DateInterval('PT' . 30 . 'M'));
}
?>