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.
The file "add.php" is actually split into two sections, one to display the initial form and the second to process form data. The $submit variable is used to identify which section to display at a given time.
There's nothing very challenging here. If the $submit variable doesn't
exist, a basic form is displayed with drop-down lists for the appointment time, and a text field for an appointment description. In order to simplify the form processing code, a number of hidden values are also passed with the visible data. PHP code is used to generate the drop-down lists, since it's faster to use a "for" loop than to create sixty <OPTION> tags (remember, you're dealing with an incredibly lazy programmer here!)
Once the user submits the form, the script is called again, but this time, the second half of the code is executed.
<?
// form not yet submitted
if (!$submit)
{
// form
}
else
{
include("config.php");
// format time
$this_time = $hh . ":" . $mm . ":00";
// set up default description
if ($comment == "") { $comment = "Not available"; }
// open a connection to the database
$connection = mysql_connect($server, $user, $pass);
// formulate the SQL query - same as above
$query = "INSERT into calendar VALUES (NULL, '$this_date', '$this_time',
'$comment')";
// run the query on the database
// assume the database is named "php101"
$result = mysql_db_query($db,$query ,$connection);
// close connection
mysql_close($connection);
header("Location: day.view.php?currYear=" . $currYear . "&currMonth=" .
$currMonth . "&currDay=" . $currDay);
}
?>
Once the form is submitted, the data entered into it is formatted as per
the data structures in the database, a query is generated, and the data is added to the database (note how "config.php" is include()d before connecting to the database to set up the necessary variables).
In case the comment field is empty, the words "Not available" are used as a default comment string. Finally, the header() function is used to redirect the browser back to the "day view" page, which displays an updated appointment list.
Here's what the result looks like.
The files "edit.php" and "delete.php" are similar - the primary difference lies in the query strings.
<?
// from edit.php
$query = "UPDATE calendar SET time='$this_time', comment='$comment' WHERE
id='$id'";
// from delete.php
$query = "DELETE FROM calendar WHERE id='$id'";
?>
The "edit.php" script also contains some extra code, used to pre-fill
the form with the details of the appointment. If you take a look at the script, you'll see that, before the form is displayed, a query is generated to obtain the appointment time and description, and this information is then used to pre-fill the text box and pre-select the hour and minute from the drop-down lists.
Here's the code to accomplish this:
<?
// form not submitted
if (!$submit)
{
?>
<html>
<head>
</head>
<body>
<?
// get data for this appointment to pre-fill form
include("config.php");
// open a connection to the database
$connection = mysql_connect($server, $user, $pass);
// formulate the SQL query - same as above
$query = "SELECT * from calendar where id='$id'";
// run the query on the database
$result = mysql_db_query($db,$query ,$connection);
while($row = mysql_fetch_array($result))
{
$this_time = explode(":", $row["time"]);
$this_date = $row["date"];
$comment = $row["comment"];
$hh = $this_time[0];
$mm = $this_time[1];
}
// close connection
mysql_close($connection);
?>
<!-- table code - snipped out à
<tr>
<td><font face=Arial size=-1><i>Time</i></font></td>
<td>
<select name="hh">
<?
// drop-down boxes for hour and time
for ($x=0; $x<=23; $x++)
{
$output = "<option value=\"" . sprintf("%02d", $x) . "\"";
// pre-select as per data
if (sprintf("%02d", $x) == $hh)
{
$output .= " selected";
}
$output .= ">" . sprintf("%02d", $x) . "</option>\n";
echo $output;
}
?>
</select>
<select name="mm">
<?
// and similar code to pre-select the minute
?>
</select>
<!-- more table code - snipped -->
</body>
</html>
<?
}
else
{
// what happens when the form is submitted
}
?>
This
article copyright Melonfire 2001. All rights reserved.