/    Sign up×
Community /Pin to ProfileBookmark

PHP Calendar… help needed!

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[/URL], feel free to check out the code.

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[/URL].

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>”;
}
?>[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@GenixdeaeJan 14.2008 — I think you're right. Maybe what you could try doing is have your query search if the date is the start of an event, if it is then maybe assign the strtotime("$start") to a session. Then you could say [code=php]if($currDate >= $_SESSION["startDate"] && $currDate <= $endDate) {//continue script[/code]. Without knowing the layout of the table and what is being outputted by the code(like your $endDate variable) its a little harder to help. Also, you don't need the "'s on your strtotime(), I'm not sure if it makes a difference with that function but some when it sees the quotes it treats the stuff contained in the quotes(so $end or $start) as a string, rather then referencing the the value assigned to that variable.
Copy linkTweet thisAlerts:
@smcl1985authorJan 14.2008 — Hi,

The start & end variables are dates, e.g

$start = "2008-06-31";
$end = "2008-07-15";


Thanks for the suggestion, i'll try it out later on hopefully it might work ?
Copy linkTweet thisAlerts:
@smickusJan 14.2008 — Just wondered... Why did you start $months[] at 1 instead of 0?
Copy linkTweet thisAlerts:
@smcl1985authorJan 14.2008 — Just wondered... Why did you start $months[] at 1 instead of 0?[/QUOTE]

Hi,

It's easier referencing the months that way - because January is the 1st month of the year, February is the 2nd, etc, etc, its simpler when your looping through the array.
Copy linkTweet thisAlerts:
@smcl1985authorJan 14.2008 — Hey again,

I've just been working on it again for the past couple of hours and have finally got it working ?

I ended up making another function that's called whenever a date needs to be checked. The function will return either "yes" or "no", and then the function will be repeatedly called until the loop finishes.

For anyone interested, i've included the below...

&lt;?php
function dateChk($currDate)
{
// select all bookings
$query = "SELECT start,end FROM bookings";
$bookinglist = @mysql_query($query);

<i> </i> // exit if error occurs
<i> </i> if(!$bookinglist)
<i> </i> exit('&lt;p&gt;Error performing query: ' . mysql_error() . '&lt;/p&gt;');

<i> </i> // convert date
<i> </i> $currDate = strtotime($currDate);

<i> </i> // loop through database entries
<i> </i> while($booking = mysql_fetch_array($bookinglist))
<i> </i> {
<i> </i> // assign data to variables
<i> </i> $start = $booking['start'];
<i> </i> $end = $booking['end'];

<i> </i> // convert dates
<i> </i> $startDate = strtotime($start);
<i> </i> $endDate = strtotime($end);

<i> </i> // check if day is booked
<i> </i> if($currDate &gt;= $startDate &amp;&amp; $currDate &lt;= $endDate)
<i> </i> $booked++;
<i> </i> }

<i> </i> // record result
<i> </i> if($booked &gt;= 1)
<i> </i> $result = "yes";
<i> </i> else
<i> </i> $result = "no";

<i> </i> // return if day is booked or not
<i> </i> return $result;
<i> </i>}

<i> </i>function calendar($year)
<i> </i>{
<i> </i> // create array of months
<i> </i> $month[1] = "January";
<i> </i> $month[2] = "February";
<i> </i> $month[3] = "March";
<i> </i> $month[4] = "April";
<i> </i> $month[5] = "May";
<i> </i> $month[6] = "June";
<i> </i> $month[7] = "July";
<i> </i> $month[8] = "August";
<i> </i> $month[9] = "September";
<i> </i> $month[10] = "October";
<i> </i> $month[11] = "November";
<i> </i> $month[12] = "December";

<i> </i> // create array for days
<i> </i> $day[0] = "S";
<i> </i> $day[1] = "M";
<i> </i> $day[2] = "T";
<i> </i> $day[3] = "W";
<i> </i> $day[4] = "T";
<i> </i> $day[5] = "F";
<i> </i> $day[6] = "S";

<i> </i> // output header and start table
<i> </i> echo "&lt;h2&gt;$year Booking Calendar&lt;/h2&gt;";
<i> </i> echo "&lt;table class="calendar" cellpadding="2px" cellspacing="1px"&gt;";
<i> </i> echo "&lt;tr class="header"&gt;&lt;td&gt;Month&lt;/td&gt;";

<i> </i> // output days in month
<i> </i> for($days = 1; $days &lt;= 31; $days++)
<i> </i> {
<i> </i> echo "&lt;td&gt;$days&lt;/td&gt;";
<i> </i> }

<i> </i> // end row
<i> </i> echo "&lt;/tr&gt;";

<i> </i> // loop through months
<i> </i> for($months = 1; $months &lt;= 12; $months++)
<i> </i> {
<i> </i> // start row
<i> </i> echo "&lt;tr&gt;";

<i> </i> // output month column
<i> </i> echo "&lt;td class="month"&gt;$month[$months]&lt;/td&gt;";

<i> </i> // generate first day of current month
<i> </i> $firstDay = date("w", mktime(0,0,0,$months,1,$year));

<i> </i> // number of days in current month
<i> </i> $daysInMonth = cal_days_in_month(0,$months,$year);

<i> </i> // initalise $currDay
<i> </i> $currDay = $firstDay;

<i> </i> // loop through days
<i> </i> for($i = 1; $i &lt;= $daysInMonth; $i++)
<i> </i> {
<i> </i> // after a week finishes, start new one
<i> </i> if($currDay == 7)
<i> </i> $currDay = 0;

<i> </i> // assign current date to variable
<i> </i> $currDate = "$year-$months-$i";

<i> </i> // assign function to variable
<i> </i> $checkingDate = dateChk($currDate);
<i> </i> $class = "sel";

<i> </i> if($checkingDate == "yes")
<i> </i> {
<i> </i> // output day as selected
<i> </i> echo "&lt;td class="sel"&gt;$day[$currDay]&lt;/td&gt;";
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> // output day of the week without class
<i> </i> echo "&lt;td&gt;$day[$currDay]&lt;/td&gt;";
<i> </i> }

<i> </i> // increment $currDay
<i> </i> $currDay++;
<i> </i> }

<i> </i> // end row
<i> </i> echo "&lt;/tr&gt;";
<i> </i> }

<i> </i> // output key to calendar
<i> </i> echo "&lt;tr&gt;&lt;td colspan="32" class="key" style="padding: 10px;"&gt;:: Key to Calendar&lt;/td&gt;&lt;/tr&gt;";
<i> </i> echo "&lt;tr&gt;&lt;td class="sel" style="height: 20px;"&gt;&lt;/td&gt;&lt;td colspan="31" style="height: 20px;"&gt;Booked Date&lt;/td&gt;&lt;/tr&gt;";
<i> </i> echo "&lt;tr&gt;&lt;td class="res" style="height: 20px;"&gt;&lt;/td&gt;&lt;td colspan="31" style="height: 20px;"&gt;Reserved Date&lt;/td&gt;&lt;/tr&gt;";

<i> </i> // end table
<i> </i> echo "&lt;/table&gt;";
<i> </i>}
?&gt;
×

Success!

Help @smcl1985 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...