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.
The page header and footer enclose the code necessary to perform the particular script's function. In the specific case of "menu.php", this code involves setting up a main menu for the user to:
view a list of current projects, with descriptions;
view a list of standard tasks, with definitions;
view or add timesheet data for any particular day;
generate reports (if administrator);
log out of the system;
Here's the code to display these options:
<?
// display page header
$title = "Main Menu";
include("header.inc.php");
?>
Please select from the following options:
<ul>
<li>
<!-- view projects option -->
<a href="projects.php">View project descriptions</a>
<p>
<li>
<!-- view tasks option -->
<a href="tasks.php">View task descriptions</a>
<p>
<li>
<!-- view timesheet option -->
<form name="view" action="view.php" method="post">
<a href="javascript:submitForm(0)">View timesheet</a> for
<? generateDateSelector(); ?>
</form>
<p>
<li>
<!-- log out option -->
<a href="logout.php">Log out of the system </a>
</ul>
<? include("footer.inc.php"); ?>
Here's what it looks like:
This is nothing but an unordered list of links, with each link pointing to a different script. Notice that the middle link requires the user to select a date, and so I've constructed it as a separate form containing a series of date selection boxes. I've also done away with the standard submit buttons in this form, preferring instead to use a simple JavaScript to submit it when its corresponding link is clicked.
In case you're wondering about the call to the generateDateSelector() function, let me explain what it does. Since I will be using the same series of drop-down boxes for date selection in numerous places, I decided to save myself some time by writing a simple PHP function to generate these boxes for me on demand. This function is stored in the include()d file "functions.php", and looks like this:
As you can see, the function includes code to automatically
pre-select the current date, month and year, together with an optional $prefix argument to customize the variable names for the three drop-down boxes.
The output of this function would look something like this
There's only
one thing missing from this menu - the link for administrators to use when generating reports. However, I need to check that the user currently logged-in is, in fact, an administrator before displaying this menu option. Let's take care of that next:
<!-- view timesheet option - snip -->
<?
// if administrator logged-in
// display report option
if ($SESSION_UPERMS == 1)
{
?>
<li>
<!-- generate report option -->
<form name="report" action="report.php" method="post">
<a href="javascript:submitForm(1)">Generate activity reports</a> between
<? generateDateSelector("s"); ?> and <? generateDateSelector("e"); ?> for
<select name="pid">
<option value="0"><all projects></option>
<?
// open connection to database
$connection = mysql_connect($hostname, $user, $pass) or die ("Unable to
connect!");
// get project list
$query = "SELECT pid, pname from projects";
$result = mysql_db_query($database, $query, $connection) or die ("Error in
query: $query. " . mysql_error());
while (list($pid, $pname) = mysql_fetch_row($result))
{
echo "<option value=$pid>$pname</option>";
}
mysql_free_result($result);
?>
</select>
</form>
<p>
<?
}
?>
<!-- log out option - snip -->
Since I would like administrators to have the ability to view
reports for a specific project, I've included a drop-down box containing a list of all current projects in the menu item above. This list is generated via a query to the "projects" table.