Time Is Money (part 2) - The Big Picture... (
Page 8 of 11 )
My general report, such as
it is, is going to look something like this:

Since I'm going to be using a table to display the
report, it helps to think of this in terms of rows and columns. The first row
contains a list of users; every subsequent row represents a new project, with
the numbers in the columns representing the total hours worked by that user on
that project. The last column display the row totals (total time spent by all
users on a specific project), while the last row contains column totals (total
time spent on all projects by a specific user).
Putting together the
first row should be simple enough - simply query the database for a list of
users and print them.
<!-- projects vs. users table -->
<table width=100% border="0" cellspacing="2" cellpadding="5">
<tr>
<td> </td>
<?
// get user list
// this resultset is useful during report generation, so make sure that it
is retained!
$query = "SELECT uid, uname FROM users";
$result = mysql_db_query($database, $query, $connection) or die ("Error in
query: $query. " . mysql_error());
// print in top row
while (list($uid, $uname) = mysql_fetch_row($result))
{
echo "<td valign=top align=center><font color=#D03468>$uname</font></td>";
}
?>
<td> </td>
</tr>
Next, I need a project list, which will be used to generate
each row:
<?
// get project list
$query2 = "SELECT pid, pname FROM projects";
$result2 = mysql_db_query($database, $query2, $connection) or die ("Error
in query: $query2. " . mysql_error());
?>
I need to create a couple of variables, one to hold the row
totals and one to hold the column totals.
<?
// create variables to hold totals
$columnTotals = array();
$rowTotal = 0;
?>
For each project (row) in this list, I need to look up the
total hours worked by each user (column) and print these numbers as cells. As I
obtain each cell total, I need to add that number to the appropriate row and
column total variable, for use at the end of every row and column respectively.
<?
// do this for each project in project list...
while (list($pid, $pname) = mysql_fetch_row($result2))
{
echo "<tr>";
echo "<td valign=top><font color=#D03468>$pname</font></td>";
$rowTotal = 0;
$count = 0;
// go back to top of user list
// select a uid...
mysql_data_seek($result, 0);
while (list($uid, $uname) = mysql_fetch_row($result))
{
// calculate the sum of the intersection of user and project
$query3 = "SELECT SUM(hours) from log WHERE pid = '$pid' AND uid =
'$uid' AND date >= '$sdate' AND date <= '$edate'";
$result3 = mysql_db_query($database, $query3, $connection) or die
("Error in query: $query3 . " . mysql_error());
list($sum) = mysql_fetch_row($result3);
// correction if sum is zero - explicitly assign it 0
if (!$sum) { $sum = 0; }
// keep track of the row total
$rowTotal = $rowTotal + $sum;
// and the column total
$columnTotals[$count] = $columnTotals[$count] + $sum;
$count++;
// print the number
echo "<td align=center>" . sprintf("%1.01f", $sum) . "</td>";
}
// end of the row, print the row total
echo "<td align=center><b>" . sprintf("%1.01f", $rowTotal) . "</b></td>";
}
echo "</tr>";
?>
Once all the rows are done, I need to add one extra row for
the column totals (the row totals have already been printed).
<tr>
<?
// last row of table contains column totals
// ie. total hours by this user on all projects
echo "<td> </td>";
// the sum of the column totals gives a grand total
$sumOfColumnTotals = 0;
// print column totals
for ($x=0; $x<sizeof($columnTotals); $x++)
{
$sumOfColumnTotals = $sumOfColumnTotals + $columnTotals[$x];
echo "<td align=center><b>" . sprintf("%1.01f", $columnTotals[$x]) .
"</b></td>";
}
// print grand total
echo "<td align=center><b>" . sprintf("%1.01f", $sumOfColumnTotals) .
"</b></td>";
// clean up resultsets
mysql_free_result($result);
mysql_free_result($result2);
mysql_free_result($result3);
?>
</tr>
</table>
Here's what it looks like:

Now, on this same page, I need to include the second
type of report, this one offering a breakdown of projects versus tasks. The code
is identical to what you've just seen - simply replace all references to users
with corresponding references to tasks.
<!-- projects versus tasks table -->
<table width=100% border="0" cellspacing="2" cellpadding="5">
<tr>
<td> </td>
<?
// get task list
// this resultset is useful further down
$query = "SELECT tid, tname FROM tasks";
$result = mysql_db_query($database, $query, $connection) or die ("Error in
query: $query. " . mysql_error());
// print tasks in top row
while (list($tid, $tname) = mysql_fetch_row($result))
{
echo "<td valign=top align=center><font color=#D03468>$tname</font></td>";
}
?>
<td> </td>
</tr>
<?
// create variable to hold column totals
$columnTotals = array();
// get project list
$query2 = "SELECT pid, pname FROM projects";
$result2 = mysql_db_query($database, $query2, $connection) or die ("Error
in query: $query2. " . mysql_error());
// for each project in list...
while (list($pid, $pname) = mysql_fetch_row($result2))
{
// go back to top of task list
mysql_data_seek($result, 0);
// print project name as first column
echo "<tr>";
echo "<td valign=top><font color=#D03468>$pname</font></td>";
$rowTotal = 0;
$count = 0;
// get intersection of this task and this project
while (list($tid, $tname) = mysql_fetch_row($result))
{
$query3 = "SELECT SUM(hours) from log WHERE pid = '$pid' AND tid =
'$tid' AND date >= '$sdate' AND date <= '$edate'";
$result3 = mysql_db_query($database, $query3, $connection) or die
("Error in query: $query3 . " . mysql_error());
list($sum) = mysql_fetch_row($result3);
// correction for zero values
if (!$sum) { $sum = 0; }
// keep track of totals for later use
$rowTotal = $rowTotal + $sum;
$columnTotals[$count] = $columnTotals[$count] + $sum;
$count++;
// print sum
echo "<td align=center>" . sprintf("%1.01f", $sum) . "</td>";
}
// print row total
echo "<td align=center><b>" . sprintf("%1.01f", $rowTotal) . "</b></td>";
}
echo "</tr>";
?>
<tr>
<?
echo "<td> </td>";
$sumOfColumnTotals = 0;
// print last row - column totals
for ($x=0; $x<sizeof($columnTotals); $x++)
{
$sumOfColumnTotals = $sumOfColumnTotals + $columnTotals[$x];
echo "<td align=center><b>" . sprintf("%1.01f", $columnTotals[$x]) .
"</b></td>";
}
// print last cell - grand total aka sum of column totals
echo "<td align=center><b>" . sprintf("%1.01f", $sumOfColumnTotals) .
"</b></td>";
// clean up resultsets
mysql_free_result($result);
mysql_free_result($result2);
mysql_free_result($result3);
?>
</tr>
</table>
And here's what the finished report looks like:

Suitable for printing or framing, eh?