Home arrow PHP arrow Page 8 - Time Is Money (part 2)

The Big Picture... - PHP

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.

TABLE OF CONTENTS:
  1. Time Is Money (part 2)
  2. Getting Creative
  3. Split Personality
  4. In...
  5. ...And Out
  6. The Number Game
  7. Exercising Restraint
  8. The Big Picture...
  9. ...And The Little Brush Strokes
  10. When Things Go Wrong
  11. Happy Endings
By: The Disenchanted Developer, (c) Melonfire
Rating: starstarstarstarstar / 4
November 16, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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?

 
 
>>> More PHP Articles          >>> More By The Disenchanted Developer, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: