Hi,
I’ve spent most of today converting a static HTML bookings calendar into a calendar that can be managed within a secure login area using PHP & MySQL.
I’ve almost finished it, but i’m having a problem looping through all of the bookings that should be displayed on the calendar. At the moment I’ve made a function that outputs a calendar, based on what year you provide it with. During the creation of the calendar it’ll connect to a database and compare each day to see if it’s booked or not, if it is it’ll put a class onto the <td> tag to change the colour from white to blue.
What’s going wrong though is it’s only outputting the last entry in the database, I guess because it keeps on overwriting each variable as its looping through the entries. I’ve tried changing about the different loops and where each is positioned within the code, but I just can’t seem to get it right!
This is my first attempt at creating a dynamic calendar, so I’m hoping someone with more experience can give me a hand ?
You can see what the calendar is suppost to look like [URL=”http://www.pinesvilla.com/availability.html”]here
I’ve made a screenshot to show what my PHP calendar is doing, which can be found [URL=”http://www.stevemclintock.com/misc/thepines-calendar.jpg”]here
The code for the calendar function can be found below…
Thanks for any help,
Steve.
[code]<?php
function calendar($year)
{
// create array of months
$month[1] = “January”;
$month[2] = “February”;
$month[3] = “March”;
$month[4] = “April”;
$month[5] = “May”;
$month[6] = “June”;
$month[7] = “July”;
$month[8] = “August”;
$month[9] = “September”;
$month[10] = “October”;
$month[11] = “November”;
$month[12] = “December”;
// create array for days
$day[0] = “S”;
$day[1] = “M”;
$day[2] = “T”;
$day[3] = “W”;
$day[4] = “T”;
$day[5] = “F”;
$day[6] = “S”;
// output header and start table
echo “<h2>$year Booking Calendar</h2>”;
echo “<table class=”calendar” cellpadding=”2px” cellspacing=”1px”>”;
echo “<tr class=”header”><td>Month</td>”;
// output days in month
for($days = 1; $days <= 31; $days++)
{
echo “<td>$days</td>”;
}
// end row
echo “</tr>”;
// loop through months
for($months = 1; $months <= 12; $months++)
{
// start row
echo “<tr>”;
// output month column
echo “<td class=”month”>$month[$months]</td>”;
// generate first day of current month
$firstDay = date(“w”, mktime(0,0,0,$months,1,$year));
// number of days in current month
$daysInMonth = cal_days_in_month(0,$months,$year);
// initalise $currDay
$currDay = $firstDay;
// loop through days
for($i = 1; $i <= $daysInMonth; $i++)
{
// after a week finishes, start new one
if($currDay == 7)
$currDay = 0;
$currDate = strtotime(“$year-$months-$i”);
/*************** SQL ***************/
// select all bookings
$query = “SELECT start,end,class FROM bookings”;
$bookinglist = @mysql_query($query);
if(!$bookinglist)
exit(‘<p>Error performing query: ‘ . mysql_error() . ‘</p>’);
while($booking = mysql_fetch_array($bookinglist))
{
$start = $booking[‘start’];
$end = $booking[‘end’];
$class = $booking[‘class’];
// convert dates
$startDate = strtotime(“$start”);
$endDate = strtotime(“$end”);
}
/*************** SQL ***************/
if($currDate >= $startDate && $currDate <= $endDate)
{
// output day of the week with class
echo “<td class=”$class”>$day[$currDay]</td>”;
}
else
{
// output day of the week without class
echo “<td>$day[$currDay]</td>”;
}
// increment $currDay
$currDay++;
}
// end row
echo “</tr>”;
}
// output key to calendar
echo “<tr><td colspan=”32″ class=”key” style=”padding: 10px;”>:: Key to Calendar</td></tr>”;
echo “<tr><td class=”sel” style=”height: 20px;”></td><td colspan=”31″ style=”height: 20px;”>Booked Date</td></tr>”;
echo “<tr><td class=”res” style=”height: 20px;”></td><td colspan=”31″ style=”height: 20px;”>Reserved Date</td></tr>”;
// end table
echo “</table>”;
}
?>