My script below reports back First available room, i want it to report 1 or 2 or 3 available rooms depending on a variable passed. please help! Thanks
This is my db table intergers:
+---+
+ i +
+---+
+ 1 +
+---+
+ 2 +
+---+
+ 3 +
+---+
<style type="text/css">
.booked
{background-color : #ff5050; color : black;}
.free {background-color : #CCFFCC;color : black; }
<font color=black size=2 face=verdana>
</style>
<?php
//book.php
$rt=1;
$sy=2005;
$sm=8;
$sd=28;
$start_date="$sy-$sm-$sd";
$ey=2005;
$em=8;
$ed=30;
$ed2=$ed-1;
$end_date="$ey-$em-$ed";
$start_date = "$sy-$sm-$sd";
// CHECK IF DESIRED BOOKING IS NOT MORE THaN 14 DAYS
$date1sp = explode("-", $start_date);
$date2sp = explode("-", $end_date);
$sec1 = date("U", mktime(0, 0, 0, $date1sp[1], $date1sp[2], $date1sp[0]));
$sec2 = date("U", mktime(0, 0, 0, $date2sp[1], $date2sp[2], $date2sp[0]));
$diffsec = $sec2-$sec1;
$diffday = round($diffsec/86400);
$maxdays=14;
if ($diffday >= $maxdays) {
echo "Booking is over 14 days, die.";
} else {
// continue with the program
include ("config.php");
MySQLConnectAndSelect();
function BuildHeader($StartDate, $EndDate, &$Array)
{
// convert times to timestamps
$sd = strtotime($StartDate);
$ed = strtotime($EndDate);
// build array of dates to search
$cd = $sd;
while ($cd < $ed)
{
$t = strftime('%Y-%m-%d', $cd);
// build header for free room array
$Array[0][$t] = 'header';
$cd = strtotime('+1 day', $cd);
}
}
function GetBookedRooms($StartDate, $EndDate, &$RoomStatus, $rt)
{
BuildHeader($StartDate, $EndDate, $RoomStatus);
$sql = 'SELECT * from intergers';
/* ***** START DIAGNOSTIC **** */
//echo '<pre>';
#print_r($sql);
echo "<BR>";
/* ***** END DIAGNOSTIC **** */
$result = mysql_query($sql) or die ('Failed to execute, Did you include rt=1?<BR><BR> ' . $sql . ' due to ' . mysql_error());
// read booked room data
while ($row = mysql_fetch_assoc($result))
{
// store room booked data in array
// 1st dimension = date room was booked
// 2nd dimension = room number
$RoomStatus[$row['room_number']][$row['thedate']] = 'Booked';
}
mysql_free_result($result);
}
function SetRoomsAvailable($StartDate, $EndDate, &$RoomStatus)
{
// convert all date/time strind to unix timestamps
$sd = strtotime($StartDate);
$ed = strtotime($EndDate);
// for each room in th array
foreach($RoomStatus as $room_number => $dates)
{
$cd = $sd;
// for each dte
while ($cd < $ed)
{
// mark room as free
$RoomStatus[$room_number][strftime('%Y-%m-%d', $cd)] = 'Free';
$cd = strtotime('+1 day', $cd);
}
}
/* ***** START DIAGNOSTIC **** */
//echo '<br><pre>';
//print_r($RoomStatus);
//echo '</pre><br>';
/* ***** END DIAGNOSTIC **** */
}
function GetFirstAvailableRoom($StartDate, $EndDate, $RoomStatus, &$Available)
{
$first_room = 0;
BuildHeader($StartDate, $EndDate, $Available);
// convert times to timestamps
$sd = strtotime($StartDate);
$ed = strtotime($EndDate);
// for each room
foreach($RoomStatus as $room_number => $dates)
{
$room_free = true;
// for each date
foreach ($dates as $date => $status)
{
$d = strtotime($date);
//checl only those dates within the required range
if ( ($sd <= $d) && ($d <= $ed) )
{
// this room is booked
if ('Booked' == $status)
{
// so abort checking this room
$room_free = false;
break;
}
}
}
// we have found a free room
if (true == $room_free)
{
// save data and break out of loop
$first_room = $room_number;
$Available[$room_number] = array();
SetRoomsAvailable($StartDate, $EndDate, $Available);
break;
}
}
// return first room found
return ($first_room);
}
function BuildTable($DataArray)
{
ksort($DataArray);
global $tdcol1;
// build display table
$tbl = '<table border="1">';
// build table header - use dates that are in the array
$tbl .= '<tr>';
$tbl .= '<td bgcolor=#ffcc99><B>Room Number</td></B>';
foreach ($DataArray[0] as $date => $status)
{
$tbl .= '<td bgcolor=#ffcc99><b>' . date('D-j-M', strtotime($date)) . '</td>';
}
$tbl .= '</tr>';
reset ($DataArray);
// for each of our rooms
foreach ($DataArray as $room_number => $room)
{
// assume row 0 is the header row
// and valid room numbers are > 0
if (0 < $room_number)
{
// create a new row and display the room number
$tbl .= '<tr>';
$tbl .= '<td><font color=navy><b>'. $room_number . '</b></td>';
// for each date in out selected range
foreach ($room as $key => $value)
{
// work out style to be used
switch ($value)
{
case 'Free':
$style = 'free';
break;
case 'Booked':
$style = 'booked';
break;
default:
$style = 'booked';
break;
}
// display the status of this room (booked or free)
$tbl .= '<td class="' . $style . '">'. $value . '</td>';
}
// end the row
$tbl .= '</tr>';
}
}
// close the table
echo"</tr></table>";
return ($tbl);
}
$roomnum=7; //amount of rooms for this type i.e 7 singles
/* ***** START OF TEST DATA ***** */
// build an array of rooms to check availability
for ($i = 1; $i <= $roomnum; $i++)
{
$room_status[$i] = array();
}
$booked = array();
SetRoomsAvailable($start_date, $end_date, $room_status);
GetBookedRooms($start_date, $end_date, $room_status, $rt);
$tbl = BuildTable($room_status);
echo $tbl;
echo"</tr></table>";
$available = array();
$first_room = GetFirstAvailableRoom($start_date, $end_date, $room_status, $available);
if (0 < $first_room)
{
echo '<h3>ROOM IS AVAILABLE FOR YOUR STAY</h3>';
$tbl = BuildTable($available);
echo $tbl;
}
}
?>