For consultancies that bill on an hourly basis - lawyers,accountants et al - time tracking is a critical part of the billingprocess. For small- and medium-size organizations, resource tracking,allocation and analysis is essential for business efficiency and planning.This article addresses both requirements by teaching you how to build atimesheet system to track and analyze work hours with PHP and MySQL.
With the main menu out of the way, let's now begin putting together the scripts corresponding to the different menu options. The first of these is the script "projects.php", which allows users to view a list of active projects.
This is a very simple script - all it needs to do is connect to the database, retrieve a list of project names and associated descriptions, and print them in a neat list.
<?
// projects.php - display project list
// includes
include("config.php");
include("functions.php");
// check for valid user session
session_start();
if(!session_is_registered("SESSION_UID"))
{
header("Location: error.php?ec=1");
exit;
}
?>
<html>
<head>
<basefont face="Verdana">
<style type="text/css">
TD {font-family: Verdana; font-size: smaller}
</style>
</head>
<body bgcolor="white">
<?
// display page header
$title = "<a style=color:white href=menu.php>Main Menu</a> > View Project
Descriptions";
include("header.inc.php");
?>
<!-- main table -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" align="left" width="60%"><b><font color="#3098C3">Project
Descriptions</font></b></td>
</tr>
<tr>
<td> </td>
</tr>
<?
// open connection to database
$connection = mysql_connect($hostname, $user, $pass) or die ("Unable to
connect!");
// get project names and descriptions
$query = "SELECT pname, pdesc FROM projects";
$result = mysql_db_query($database, $query, $connection) or die ("Error in
query: $query. " . mysql_error());
// iterate through resultset and display
while (list($pname, $pdesc) = mysql_fetch_row($result))
{
echo "<tr><td><ul><b><li>$pname</b><br>$pdesc</td>";
echo "<tr><td> </td>";
}
// close connection
mysql_close($connection);
?>
</table>
<? include("footer.inc.php"); ?>
</body>
</html>
It looks simple, and it is. Here's what the output looks
like:
Almost identical in function is the script "tasks.php", which displays a list of standard tasks, together with definitions for each. The only difference lies in the query that is used - as you can see:
<?
// tasks.php - display task list
// includes
// check for valid user session
// display page header
// open connection to database
$connection = mysql_connect($hostname, $user, $pass) or die ("Unable to
connect!");
// get project names and descriptions
$query = "SELECT tname, tdesc FROM tasks";
$result = mysql_db_query($database, $query, $connection) or die ("Error in
query: $query. " . mysql_error());
// iterate through resultset and display
while (list($tname, $tdesc) = mysql_fetch_row($result))
{
echo "<tr><td><ul><b><li>$tname</b><br>$tdesc</td>";
echo "<tr><td> </td>";
}
// close connection
mysql_close($connection);
// include page footer
?>