\n";
// If called from a submit get posted data else take todays date
if( strcmp($fileext,"php")==0 )
{
$calledfromphp = true;
// Input db query params from booking form
$property = $_POST['property'];
$monthnum = $_POST['monthnum'];
$year = $_POST['year'];
}
// Output variables
$querystate = QS_SUCCESS;
$bookeddates = array();
// Local variables
$booking = array();
// Check input variables
// Year
$iyear = (int) $year;
if( ($iyear < 2007) || ($iyear > 2020) )
{
$querystate = QS_YEARRANGE;
echo $querystate ;
return;
}
// Month
$imonth = (int) $monthnum;
if( ($imonth < 1) || ($imonth > 12) )
{
$querystate = QS_MONTHRANGE;
// echo $querystate ;
return;
}
// Set up month map arrays
$monthname = array("January","February","March","April","May","June",
"July","August","September","October","November","December");
$monthdays = array(31,28,31,30,31,30,31,31,30,31,30,31);
$lastdayidx = ((int) $monthnum) - 1;
$isleap = false;
if( ($year % 4) == 0 )
$isleap = true;
// Arrays indexed from zero so Feb is 1
if( $isleap && ($lastdayidx==1) )
$ilastday = $monthdays[$lastdayidx]+1;
else
$ilastday = $monthdays[$lastdayidx];
// Connect to the database
$link = mysql_connect($host,$user,$password);
if (!$link) {
$querystate = QS_CONNECTIONFAILED;
die("Could not connect: " . mysql_error());
}
//echo "Connected successfully." . "
\n";
$seldb = mysql_select_db($database);
if(!$seldb) {
$querystate = QS_DBSELECTFAILED;
die("Could not select DB: " . mysql_error());
}
//echo "Selected DB successfully." . "
\n";
// Set up BOOKTO access variables
$booktofirst= $year ."-" . $monthnum . "-" . $firstday;
$booktolast= $year ."-" . $monthnum . "-" . $ilastday;
echo "Booktofirst query date : " . $booktofirst . "
\n";
echo "Booktolast query date : " . $booktolast . "
\n";
// Query bookings set date access
$query = sprintf("SELECT bookfrom,bookto FROM bookings WHERE property ='%s' AND bookto >='%s' AND bookto <='%s'",
mysql_real_escape_string($property),
mysql_real_escape_string($booktofirst),
mysql_real_escape_string($booktolast));
// Make query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
$querystate = QS_QUERYFAILED;
die($message);
}
//echo "Query successful." . "
\n";
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
extract($row, EXTR_OVERWRITE );
$booking[] = array( $bookfrom, $bookto );
echo "Bookfrom : " . $row['bookfrom'] . "
\n";
echo "Bookto : " . $row['bookto'] . "
\n";
}
// Set up BOOKFROM access variables
$bookfromfirst= $year ."-" . $monthnum . "-" . $firstday;
$bookfromlast= $year ."-" . $monthnum . "-" . $ilastday;
echo "Bookfromfirst query date : " . $bookfromfirst . "
\n";
echo "Bookfromlast query date : " . $bookfromlast . "
\n";
// Query bookings set date access
$query = sprintf("SELECT bookfrom,bookto FROM bookings WHERE property ='%s' AND bookfrom >='%s' AND bookfrom <='%s'",
mysql_real_escape_string($property),
mysql_real_escape_string($bookfromfirst),
mysql_real_escape_string($bookfromlast));
// Make query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
$querystate = QS_QUERYFAILED;
die($message);
}
//echo "Query successful." . "
\n";
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
extract($row, EXTR_OVERWRITE );
$booking[] = array( $bookfrom, $bookto );
echo "Bookfrom : " . $bookfrom . "
\n";
echo "Bookto : " . $bookto . "
\n";
}
// Bookings are now in array and booking and can be sorted
$ithismonth = (int) $monthnum;
if( count($booking) == 0 )
{
$querystate = QS_NORESULTS;
echo $querystate ;
}
if( $querystate == QS_SUCCESS )
{
$busydates = checkbookings( $booking, $ithismonth, $ilastday );
// Remove duplicate dates - should not really exist
$bookeddates = array_unique($busydates);
echo "Final booked dates ..." . "
\n";
for( $i=0; $i < count($bookeddates); $i++ )
echo "Final Date : " . $bookeddates[$i] . "
\n";
}
// Close the database connection
mysql_close($link);
// Build javascript calendar for the following date
$querydate = $monthnum . "/1". "/" . $year ;
//echo "Query date : " . $querydate ;
echo "";
function checkbookings($booking, $ithismonth, $ilastday)
{
$monthdates = array();
echo "Sorting bookings ... " . "
\n";
for( $i=0; $i < count($booking); $i++ )
{
//echo "Bookfrom : " . $booking[$i][0] . "
\n";
//echo "Bookto : " . $booking[$i][1] . "
\n";
// Test for bookings starting last month ending this
$ibookfrommonth = (int) substr( $booking[$i][0], strpos($booking[$i][0], "-")+1, 2 );
$ibooktomonth = (int) substr( $booking[$i][1], strpos($booking[$i][1], "-")+1, 2 );
// echo "ibookfrommonth : " . $ibookfrommonth . "
\n";
// echo "ibooktomonth : " . $ibooktomonth . "
\n";
// echo "ithismonth : " . $ithismonth . "
\n";
if( ($ibookfrommonth < $ithismonth) && ($ibooktomonth == $ithismonth) )
{
$ibooktoday = (int) substr( $booking[$i][1], strrpos($booking[$i][1], "-")+1, 2 );
$monthbooking[] = array( 1, $ibooktoday);
}
// Test for bookings within the month
if( ($ibookfrommonth >= $ithismonth) && ($ibooktomonth <= $ithismonth) )
{
$ibookfromday = (int) substr( $booking[$i][0], strrpos($booking[$i][0], "-")+1, 2 );
$ibooktoday = (int) substr( $booking[$i][1], strrpos($booking[$i][1], "-")+1, 2 );
$monthbooking[] = array( $ibookfromday, $ibooktoday);
}
// Test for bookings starting this month ending next
if( ($ibookfrommonth <= $ithismonth) && ($ibooktomonth > $ithismonth) )
{
$ibookfromday = (int) substr( $booking[$i][0], strrpos($booking[$i][0], "-")+1, 2 );
$monthbooking[] = array( $ibookfromday, $ilastday);
}
// Test for bookings which include the whole month
if( ($ibookfrommonth < $ithismonth) && ($ibooktomonth > $ithismonth) )
{
$ibookfromday = 1;;
$monthbooking[] = array( $ibookfromday, $ilastday);
}
}
// Generate list of booked dates
// echo "Truncated bookings ... count:" . "
\n";
for( $i=0; $i < count($monthbooking); $i++ )
{
for( $j=$monthbooking[$i][0]; $j <= $monthbooking[$i][1]; $j++ )
$monthdates[] = $j;
}
return $monthdates;
}
?>
All images, video and video soundtracks are Copyright © 2008. All rights reserved.