Miles To Go Before I Sleep... - Adding, Editing, Deleting... (
Page 5 of 6 )
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.
<?
// form not yet submitted
if (!$submit)
{
// format date
$this_date = $currYear . "-" . sprintf("%02d", $currMonth) . "-" .
sprintf("%02d", $currDay);
?>
<html>
<head>
</head>
<body>
<table border="0" cellpadding="2" cellspacing="2">
<form action="<? echo $PHP_SELF; ?>" method="get">
<tr>
<td colspan=2 align=center>
<font face=Arial size=-1>
<b><? echo date("D M d Y", mktime(0,0,0,$currMonth,$currDay,$currYear));
?></b>
</font>
</td>
</tr>
<tr>
<td>
<font face=Arial size=-1>
<i>Time</i>
</font>
</td>
<td>
<select name="hh">
<?
// drop-downs for the date and time
for ($x=0; $x<=23; $x++)
{
echo "<option value=\"" . sprintf("%02d", $x) . "\">" . sprintf("%02d",
$x) . "</option>\n";
}
?>
</select>
<select name="mm">
<?
for ($x=0; $x<=59; $x++)
{
echo "<option value=\"" . sprintf("%02d", $x) . "\">" . sprintf("%02d",
$x) . "</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<font face=Arial size=-1>
<i>Description</i>
</font>
</td>
<td><input type="Text" name="comment" size="15"></td>
</tr>
<input type=hidden name=this_date value=<? echo $this_date; ?>>
<input type=hidden name=currYear value=<? echo $currYear; ?>>
<input type=hidden name=currMonth value=<? echo $currMonth; ?>>
<input type=hidden name=currDay value=<? echo $currDay; ?>>
<tr>
<td colspan=2 align=center>
<input type="Submit" name="submit" value="Add Entry">
</td>
</tr>
</form>
</table>
</body>
</html>
<?
}
else
{
// form data is processed here
}
?>
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.