Ever wished you had an appointment calendar you could accessthrough your Web browser? Well, it's not too late. This week, learn how tobuild a simple PHP-based appointment calendar which allows you to view,add, edit and delete appointments and meetings. And if you're just gettinginto PHP, this article will show you how to apply the theory you've spentso much time learning to a real-world application.
There's one more thing you can do to make this calendar a little more useful - display an indicator in "month view" to identify which days already have appointments scheduled, and which days are completely free of appointments. In order to do this, go back to "month.view.php" and add the following lines of code to it, somewhere near the beginning of the script (but after you've defined $currDay, $currMonth, and $currYear):
<?
include("config.php");
// open a connection to the database
$connection = mysql_connect($server, $user, $pass);
// formulate the SQL query - same as above
$query = "SELECT DISTINCT date from calendar where date >= '" . $currYear .
"-" . sprintf("%02d", $currMonth) . "-01' and date <= '" . $currYear . "-"
. sprintf("%02d", $currMonth) . "-" . $totalDays[$currMonth] . "'";
// run the query on the database
$result = mysql_db_query($db,$query ,$connection);
$x=0;
$dateList=array();
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
$dates = explode("-", $row["date"]);
$dateList[$x] = $dates[2];
$x++;
}
}
// close connection
mysql_close($connection);
?>
What have I done here? I've used the three variables to formulate a query
which returns a list of all the days in $currMonth which already have appointments scheduled (the DISTINCT keyword helps to eliminate duplicate entries). Then I've taken each of those date strings (in the form YYYY-MM-DD), split them into separate entities, and created an array called $dateList which contains a list of all the days on which appointments are scheduled.
The plan is to add an additional check to the sections of code responsible for generating the dates in the month, such that dates which match the elements in the $dateList array have an additional identifier to indicate that something is already scheduled for that day.
You saw earlier that I had separated the date and time into separate columns when creating the database table. One of the primary reasons behind this was to simplify the task of obtaining a list of dates which had one or more appointments scheduled. If the date and time had been combined into a single column, the DISTINCT keyword would have failed to eliminate duplicate entries, and I would have had to write a lot more code to weed out the duplicates. And we already know how lazy I am...
<?
// counter to track the current date
$dayCount=1;
while ($dayCount <= $totalDays[$currMonth])
{
// use this to find out when the
// 7-day block is complete and display a new
row
if ($rowCount % 7 == 0)
{
echo "</tr>\n<tr>\n";
}
// if today, display in different colour
// print date
if ($dayCount == date("j") && $currYear == date("Y") && $currMonth ==
date("n"))
{
echo "<td align=center bgcolor=Silver><font face=Arial size=-1><a
href=day.view.php?currYear=" . $currYear . "&currMonth=" . $currMonth .
"&currDay=" . $dayCount . ">" . $dayCount. "</a></font>";
}
else
{
echo "<td align=center><font face=Arial size=-1><a
href=day.view.php?currYear=" . $currYear . "&currMonth=" . $currMonth .
"&currDay=" . $dayCount . ">" . $dayCount . "</a></font>";
}
// newly-added code to find out is appointment is already scheduled
// and print indicator if so
for ($y=0; $y<sizeof($dateList); $y++)
{
//echo $dateList[$y];
if ($dateList[$y] == $dayCount)
{
echo "<font face=Arial color=red size=-4>+</font>";
}
}
echo "</td>\n";
// increment counters
$dayCount++;
$rowCount++;
}
?>
And here's what the finished product looks like:
And that's about it. You can now begin using this calendar, as is, for keeping track of your life; modify it as per your requirements; or use it as an entry point to other applications. And if you're new to PHP, this tutorial should hopefully have offered you some insight into how Web applications are developed, and maybe even sparked some ideas of your own. If so, let me know...and till next time, stay healthy!
This article copyright Melonfire 2001. All rights reserved.