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 :
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>
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>
bat , thanks for your answer, I tried to execute your code with an array set like this :
$array = 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');
but it's display the table like this (all room is booked) :
how I can display the hours...
*if the image not shown, this is the table displayed :
a | booked
b | booked
c | booked
d | booked
e | booked
f | booked