Jump to content
Larry Ullman's Book Forums

Online Booking System

Recommended Posts

I would like to add a simple booking system to a holiday cottage site. Is this very difficult to build from scratch? I've looked for some online tutorials but haven't found any. I would like to build this rather than purchase a system, so any suggestions on how to approach this or for online help would be great.

Link to comment
Share on other sites

Thanks Larry, any pointers re approach and database design would be great!


I have 2 projects. I'd like to try doing this from scratch as I think it will be a good learning exercise.


1. Display a calendar of availability for one property. Ideally a monthly calendar will be displayed with the background colour coded to indicate available, not available, pending. This image is the general idea, http://www.google.co...,r:13,s:0,i:113


The rental fee will vary depending on time of year and length of booking. Booking period can be for any number of days, not just per week. Booking periods can begin and end on any day. The initial display will show the current month with a little arrow on the top right corner to link to the next calendar month which will then have forward and backward arrows. The visitor will be able to click the arrows to go back and forth through calendar months to check availability. Then a form will collect booking information from the user and check that the requested dates are indeed available and email the owner the request.


2. Room booking system - multiple rooms for set periods during the day. Display similar to above but by week.

Link to comment
Share on other sites

The display is tricky to pull off, but just a matter of generating the right HTML. What I normally do is have the site owner/admin enter, through a form, the available days and costs. Then public users can book available days. With what you describe, it depends upon how costs are determined. For example, if each day can have its own price but 2 days together gets 5% off, three days 10%, etc.

Link to comment
Share on other sites

My approach is to get all the booked dates from the d/b and store them in an array. Then I display the calendar with forward and backward arrows to allow the visitor to page through the months. Using some for and while loops, I check if each calendar date is in the booked_dates array and set a css class accordingly.


The basic functionality is working so I will look at how I set the booking price but for now I wonder if you would comment on my code. I'm not sure how efficient it is - I've had to do alot of date/variable formatting and wonder if there are better solutions. I know its alot of code so if you don't want to go through it, I Many thanks for any comments.

$r = mysqli_query($dbc, "SELECT start_date, num_days FROM bookings ORDER BY start_date");
while (list($start, $num_days) = mysqli_fetch_array($r, MYSQLI_NUM)){
//use number of days for loop count
for ($i=0; $i<$num_days; $i++) {
	if ($i==0){
		$days_booked[] = $start;
	else {
	// separate date into year, month and day to add $i days to day
		$date_parts = explode('-',$start);
		$days_booked[] = date("Y-m-d",mktime(0,0,0, ($date_parts[1]), $date_parts[2] + $i, ($date_parts[0])));
} //end for loop
} //end while loop

// check if month and year have been passed in url i.e. forward or backward arrow on calendar date has been clicked
$mOptions = array('min_range' => 1, 'max_range' => 12);
$yOptions = array('min_range' => 2012, 'max_range' => 2020);
if (isset($_GET['m']) && isset($_GET['y']) ) {
$month = filter_var($_GET['m'], FILTER_VALIDATE_INT, $mOptions);
$year = filter_var($_GET['y'], FILTER_VALIDATE_INT, $yOptions);

//forward arrow clicked	
if (isset($_GET['a']) && ($_GET['a'] == 'f') ){
$year= ($month == 12 ? $year + 1: $year);
$month = ($month == 12 ? 01 : $month + 1);

//backward arrow clicked
if (isset($_GET['a']) && ($_GET['a'] == 'b') ){
$year= ($month == 1 ? $year - 1: $year);
$month = ($month == 01 ? 12 : $month - 1);
$month =($month < 10 ? str_pad($month, 2, "0", STR_PAD_LEFT) : $month);

else {
//else first time on page, get today's date to start display on current month
$date = time ();
//assign day, month, and year to separate variables
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);

//generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
//get the month name
$title = date('F', $first_day) ;

//determine how many blank squares to display at start of month
$day_of_week = date('D', $first_day);
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;

//get num of days in the current month
$days_in_month = cal_days_in_month(0, $month, $year);
//display the table header
echo "<table border=1 width=594 height=394>";
echo "<thead><tr><th colspan=7> <a href=\"calendar.php?m=$month&y=$year&a=b\"><span class=\"bArrow\"><</span></a>$title $year <a href=\"calendar.php?m=$month&y=$year&a=f\"><span class=\"fArrow\">></span></a></th></tr></thead>";
echo "<tr><td width=42 align=\"center\">Sun</td><td width=42 align=\"center\">Mon</td><td
width=42 align=\"center\">Tue</td><td width=42 align=\"center\">Wed</td><td width=42 align=\"center\">Thu</td><td
width=42 align=\"center\">Fri</td><td width=42 align=\"center\">Sat</td></tr>";
//keep count of the days in the week, up to 7
$day_count = 1;
echo "<tr>";
//display blank days
while ( $blank > 0 ) {
 echo "<td></td>";
 $blank = $blank-1;

//set the first day of the month to 1
$day_num = 1;
//count up the days in the month
while ($day_num <= $days_in_month ) {

//format calendar day to be same format as date in days_booked array
$day_num = str_pad($day_num, 2, "0", STR_PAD_LEFT);
//concatenate year, month, day for comparison
$compare_date = "$year-$month-$day_num";
//set css class variable
$avail = (in_array($compare_date,$days_booked) ? 'unavail' : 'avail');
 //display calendar day with appropriate css class
 echo "<td class=\"$avail\">$day_num</td>";
 //start a new row every week
 if ($day_count > 7) {
	 echo "</tr><tr>";
	 $day_count = 1;

//finish the table with some blank squares if needed
while ( $day_count >1 && $day_count <=7 ) {
 echo "<td> </td>";
echo "</tr></table>";

Link to comment
Share on other sites


  • Create New...