PHP
  Home arrow PHP arrow Page 8 - Time Is Money (part 2)
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Time Is Money (part 2)
By: The Disenchanted Developer, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2001-11-16

    Table of Contents:
  • Time Is Money (part 2)
  • Getting Creative
  • Split Personality
  • In...
  • ...And Out
  • The Number Game
  • Exercising Restraint
  • The Big Picture...
  • ...And The Little Brush Strokes
  • When Things Go Wrong
  • Happy Endings

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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>&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


     

       

    PHP ARTICLES

    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT