In this concluding article, explore the scripts which add andremove timesheet entries to the system, and get a crash course instatistics by using these entries to generate useful resource allocationand usage reports.
If you look at the main menu, you'll see that the third menu item allows the user to view entries for a specified date. When you analyze this a little, though, you'll see that this menu item has broader implications - it must allow the user to:
view a list of timesheet entries for the specified date;
add new entries for the specified date;
delete existing entries from the list.
In order to perform these functions, the script - I've called it "view.php" - must necessarily receive a valid datestamp (created from the variables $d, $m and $y). These three variables are generated from the drop-down boxes in "menu.php" - you may remember this from last time's article (if not, I'd strongly suggest you review the source code for "menu.php" and then come back here). Once "view.php" receives these values, it generates a datestamp, checks to ensure that the date is a valid one, and then queries the "log" table for all relevant data.
So that takes care of displaying existing entries. But what about adding new ones, or deleting existing ones?
Well, after much thought and a couple of conversations with our resident interface designer, I've come up with a clever way to combine both these functions into "view.php". Here's a rough sketch of what I have in mind:
Essentially, I plan to split the page into two main sections. The left side of the page will contain a list of entries retrieved from the database, with a checkbox next to each; the user can select an entry for deletion by checking the corresponding box. The right side of the page will hold a form, which can be used to add new entries to the timesheet. Entries added from the form on the right side will immediately appear in the list on the left side.
This sounds complicated, but it's actually not all that difficult to implement. The important thing here is to take the pieces one at a time and deal with them separately. And so, the first thing I need to do is set up a page template.
<?
// view.php - view/add/delete entries for a specific date
// includes
// check for valid user session
session_start();
if(!session_is_registered("SESSION_UID"))
{
header("Location: error.php?ec=1");
exit;
}
// check for valid date
if (!checkdate($m, $d, $y))
{
header("Location: error.php?ec=2");
exit;
}
<html>
<head>
</head>
<body bgcolor="white">
<?
// display page header
?>
<!-- table (1r, 3c) -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" align="left" width="60%">
<!-- table for existing timesheet records goes here - snip -->
</td>
<!-- spacer -->
<td width="10%">
</td>
<td valign="top" align="left" width="30%">
<!-- table for new timesheet records goes here - snip -->
</td>
</tr>
</table>
<?
// display page footer
?>
</body>
</html>
The script begins with the usual checks and includes, and
generates an HTML page containing a (1r, 3c) table. The first and last of these cells will be used for the existing and new timesheet data respectively.
Note the checkdate() function at the top of the script - I'm using this to validate the date value received by "view.php". This check is necessary to catch incorrect date values - for example, 30 February or 31 April. If the date selected by the user is invalid, the script will redirect to the generic error handler, which should display a message indicating the error.