Jump to content

Availibility calendar and prices - how do I go about it?


richclever

Recommended Posts

Hi all, first post so bear with me.

 

I have a site that has an availability calendar which is constructed in php.  Booked dates are held in a separate Mysql table and works fine.

 

I also want to have it so when a customer hovers over specific dates it shows up the price for that date and this is where I am getting stuck.

 

All the dates are held in the main property table in the form of beginning and end dates for the booking period (low, mid, high and peak) and also have prices allocated to them (ie mid season has it's beginning and end date and also a field that holds the price).  I presume that I need to query the date to find out what season it is in and then pull the price out from the database so as to display it.  Do I need a multidimensional array and if so how would I go around constructing it?

 

Hope that makes sense.

 

Thanks,

 

Richard

I'm not sure how to help you specifically, since you haven't presented any code or a url where we can see the calendar, but yes - when you detect which date (cell in a table?) the user is hovering over then you just need to look up which season the date belongs to and then return the price...

Hi,

 

This is the code that I use to construct the calendar.  First the function:

 

function generate_calendar($year, $month, $days = array(), $day_name_length = 3, $month_href = null, $first_day = 0, $pn = array(), $monthdis)
{
			$first_of_month = gmmktime(0, 0, 0, $month, 1, $year); 
			// remember that mktime will automatically correct if invalid dates are entered
			// for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
			// this provides a built in "rounding" feature to generate_calendar()
			$day_names = array(); #generate all the day names according to the current locale
			for($n = 0, $t = (3 + $first_day) * 86400; $n < 7; $n++, $t += 86400) // January 4, 1970 was a Sunday
			$day_names[$n] = ucfirst(gmstrftime('%A', $t)); #%A means full textual day name

			list($month, $year, $month_name, $weekday) = explode(',', gmstrftime('%m,%Y,%B,%w', $first_of_month));
			$weekday = ($weekday + 7 - $first_day) % 7; #adjust for $first_day
			$title = htmlentities(ucfirst($month_name)) . ' ' . $year; #note that some locales don't capitalize month and day names

			// Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03
			@list($p, $pl) = each($pn);
			@list($n, $nl) = each($pn); #previous and next links, if applicable
			if ($monthdis != "1") {
							if ($p) $p = '<p><span class="calendar-prev">' . ($pl ? '<a href="' . htmlspecialchars($pl) . '">' . $p . '</a>' : $pn) . '</span>';
							if ($n) $n = ' <span class="calendar-next">' . ($nl ? '<a href="' . htmlspecialchars($nl) . '">' . $n . '</a>' : $n) . '</span></p><br /><br />';



							$calendar = '<div class="calendar">' . "\n" . '<span class="calendar-month" style="font-size:12px;">' . $p . ($month_href ? '<a href="' . htmlspecialchars($month_href) . '">' . $title . '</a>' : $title) . $n . "</span><br />\n<span>";
			} else {

							$calendar = '<div class="calendar">' . "\n" . '<span>';



			} ; 
			// if($day_name_length){ #if the day names should be shown ($day_name_length > 0)
			// if day_name_length is >3, the full name of the day will be printed
			// foreach($day_names as $d)
			// $calendar .= '<span abbr="'.htmlentities($d).'">'.htmlentities($day_name_length < 4 ? substr($d,0,$day_name_length) : $d).'</span>';
			// $calendar .= "</span>\n<span>";
			// }
			if ($weekday > 0) ; #initial 'empty' days
			for($day = 1, $days_in_month = gmdate('t', $first_of_month); $day <= $days_in_month; $day++, $weekday++) {
							// if($weekday == 7){
							// $weekday   = 0; #start a new week
							// $calendar .= "</span>\n<span>";
							// }
							if (isset($days[$day]) and is_array($days[$day])) {
											@list($link, $classes, $content) = $days[$day];
											if (is_null($content)) $content = $day;
											$calendar .= '<span' . ($classes ? ' class="' . htmlspecialchars($classes) . '" style="width:17px; font-size:12px;">' : '>') .
											($link ? '<a href="' . htmlspecialchars($link) . '">' . $content . '</a>' : $content) . '</span>';
							} else
							if ($day < 10) {

									$calendar .= "<span class=\"calendar1\" style=\"width:17px; font-size:12px;\" >0$day</span>";
							} else {
											$calendar .= "<span class=\"calendar1\" style=\"width:17px; font-size:12px;\">$day</span>
							} 
			} 
			if ($weekday != 7) $calendar .= '<span  style="width:19px"> </span>'; #remaining "empty" days

			return $calendar . "</span>\n</div>\n";
} 

 

And now where it is called in the page:

 

<div class="calleft"><br /><br /><br /><a name="Villaspools" id="Villaspools">
<div style="float:left;margin-top:-10px;"></a>Villas with Pools</div><br />
<?php
$caltitsql = mysql_query("select * from properties where active='1' and (type1='wp' or type2='wp' or type3='wp' or type4='wp' or type5='wp' or type6='wp' or type7='wp' or type8='wp' )order by bedrooms"  )or die(mysql_error());
while ($calsqltitresult = mysql_fetch_array($caltitsql)) {

			?><p><a href="Villa_<?php echo $calsqltitresult['ref'];
			?>.htm"><?php 
			$oRIGINAL = $calsqltitresult['title'];
			$sHORTER = substr_replace($oRIGINAL, '...', 25);
			echo $calsqltitresult['ref'].": ".$calsqltitresult['bedrooms']." Bedrooms"?>
			</a></p>
<?php
} 

        ?></div><?php
$calhorsql = mysql_query("select * from properties where active='1'and (type1='wp' or type2='wp' or type3='wp' or type4='wp' or type5='wp' or type6='wp' or type7='wp' or type8='wp' )order by bedrooms")or die(mysql_error());
while ($calsqlhoresult = mysql_fetch_array($calhorsql)) {
			$pid = $calsqlhoresult['id'];

			$sql = mysql_query("select * from calendar where id = '$pid'");

			while ($result = mysql_fetch_row($sql)) {
							$datepieces = explode("-", $result[2]);
							$datemonth = (int)$datepieces[1];
							$dateyear = (int)$datepieces[0];

							if ($result[3] == "B" and $datemonth == $dismonth) {
											$dateday = (int)$datepieces[2];
											$days[$dateday] = array('', 'booked-day', ' B ');
							} elseif ($result[3] == "U" and $datemonth == $dismonth) {
											$dateday = (int)$datepieces[2];
											$days[$dateday] = array('', 'unavailable-day', ' U ');
							} 


			} 
			if (!$today) {
							$today = getdate();
							$todayyear = $today['year'];
							$todaymonth = $today['mon'];
							$todayday = $today['mday'];
			} else {
							$todaymonth++;
			} 

			if (isset ($dismonth)) {
							$todaymonth = $dismonth;
							$nextmon = $todaymonth;
							$nextmon++;
							$prevmon = $todaymonth -1;
							$premonurl = "?dismonth=" . $prevmon;
							$nextmonurl = "?dismonth=" . $nextmon ;
			} else {
							$todaymonth = $today['mon'];
							$nextmon = $todaymonth;
							$nextmon++;
							$prevmon = $todaymonth -1;
							$premonurl = "?dismonth=" . $prevmon . "&pid=" . $pid;
							$nextmonurl = "?dismonth=" . $nextmon . "&pid=" . $pid;
			} 
			$pn = array('«' => $premonurl, '»' => $nextmonurl);

?>
<div class="calrt">

	<p><?php

			echo generate_calendar($todayyear, $todaymonth, $days, 3, null, 0, $pn, $monthdisplayed);
	unset($days);
			?></p>
</div>

	<?php
			$monthdisplayed = 1;
	} 

?>

 

Hope this gives you a better idea of how it works at present.  Oh and sorry about the code formatting

 

Richard

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.