View Single Post
  #1  
Old 8th Apr 04, 01:39 PM
JacKDynne's Avatar
JacKDynne JacKDynne is offline
Administrator
 
Join Date: Oct 2001
Location: The Past Through Tomorrow
Posts: 1,591
JacKDynne will become famous soon enoughJacKDynne will become famous soon enough
Send a message via MSN to JacKDynne
Here's my problem:

I have a web based timesheet app running with php and mysql. When I run reports on specified date ranges, it pulls the entire user db (not just the department that runs the report) and puts it in a row across the top which is really wide (I have to scroll over from the bottom). What happens is that unless I copy the entire page and insert into an excel file the majority of the users are cut off when I print straight from the browser. Big problem 'cause my users are not literate enough to cut and paste as I did.

Heres the questions:

1. Is there any way to have the html print correctly (either by wrapping or some other means)?

2.What could I include in the report to only print users time from specific departments? (doesn't really fix the problem for big departments though)?

I am trying to flip the Users/Tasks sections so that the tasks are printed in the top row then the users are printed from top to bottom but no luck getting it right thus far...

Any input is appreciated *TIA*

The code for the report is included below and here is a screenshot of the generated report:

Code:
<? // report.php - generate reports // includes include("config.php"); include("functions.php"); // assign global variables for PHP 4.1+ with register_globals disabled $sm = $_REQUEST['sm']; $sd = $_REQUEST['sd']; $sy = $_REQUEST['sy']; $em = $_REQUEST['em']; $ed = $_REQUEST['ed']; $ey = $_REQUEST['ey']; $pid = $_REQUEST['pid']; $pname = $_REQUEST['pname']; // check for valid session and valid administrator session_start(); if(!session_is_registered("SESSION_UID") || $_SESSION['SESSION_UPERMS'] > 2 ) { header("Location: error.php?ec=1"); exit; } // check for valid dates if (!checkdate($sm, $sd, $sy) || !checkdate($em, $ed, $ey)) { header("Location: error.php?ec=2"); exit; } // open connection to database $connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!"); // create start and end datestamp $sdate = $sy . "-" . $sm . "-" . $sd; $edate = $ey . "-" . $em . "-" . $ed; ?> <html> <head> <basefont face="Verdana"> <style type="text/css"> TD {font-family: Verdana; font-size: smaller} </style> </head> <body bgcolor="FFFF99"> <? // display page header $title = "<a style=color:white href=menu.php>Main Menu</a> > Generate Activity Reports"; include("header.inc.php"); ?> <? // main "if" loop begins // if report required for ALL projects if ($pid == 0) { ?> <!-- main table &nbsp;--> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <!-- title --> <tr> <td valign="top" align="left"> <b><font color="#3098C3">Activity report for all projects for the period <? echo fixDate($sdate); ?> to <? echo fixDate($edate); ?></font></b> </td> </tr> <!-- spacer --> <tr> <td>&nbsp;</td> </tr> <!-- begin projects vs. users section --> <!-- begin projects vs. users section --> <tr> <td valign="top" align="left"> <b><font color="#3098C3">&nbsp;&nbsp;Projects/Users:</font></b> </td> </tr> <tr> <td valign="top" align="left"> <!-- 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()); &nbsp;// print in top row &nbsp;while (list($uid, $uname) = mysql_fetch_row($result)) &nbsp;{ &nbsp;echo "<td valign=top align=center><font color=#D03468>$uname</font></td>"; &nbsp;} ?> <td>&nbsp;</td> </tr> <? // create a 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()); &nbsp;// do this for each project in project list... &nbsp;while (list($pid, $pname) = mysql_fetch_row($result2)) &nbsp;{ &nbsp;echo "<tr>"; &nbsp;echo "<td valign=top><font color=#D03468>$pname</font></td>"; &nbsp;$rowTotal = 0; &nbsp;$count = 0; &nbsp; &nbsp;// go back to top of user list &nbsp;// select a uid... &nbsp;mysql_data_seek($result, 0); &nbsp; while (list($uid, $uname) = mysql_fetch_row($result)) &nbsp; { &nbsp; // calculate the sum of the intersection of user and project &nbsp; $query3 = "SELECT SUM(hours) from log WHERE pid = '$pid' AND uid = '$uid' AND date >= '$sdate' AND date <= '$edate'"; &nbsp; $result3 = mysql_db_query($database, $query3, $connection) or die ("Error in query: $query3 . " . mysql_error()); &nbsp; list($sum) = mysql_fetch_row($result3); &nbsp; &nbsp; &nbsp;// correction if sum is zero - explicitly assign it 0, so that it gets printed in report &nbsp; &nbsp;if (!$sum) { $sum = 0; } &nbsp; &nbsp; // keep track of the row total &nbsp; $rowTotal = $rowTotal + $sum; &nbsp; &nbsp; // and the column total &nbsp; $columnTotals[$count] = $columnTotals[$count] + $sum; &nbsp; $count++; &nbsp; &nbsp; // print the number &nbsp; echo "<td align=center>" . sprintf("%1.02f", $sum) . "</td>"; &nbsp; } &nbsp;// end of the row, print the row total &nbsp;echo "<td align=center><b>" . sprintf("%1.02f", $rowTotal) . "</b></td>"; &nbsp;} echo "</tr>"; ?> <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; &nbsp;// print column totals &nbsp;for ($x=0; $x<sizeof($columnTotals); $x++) &nbsp;{ &nbsp;$sumOfColumnTotals = $sumOfColumnTotals + $columnTotals[$x]; &nbsp;echo "<td align=center><b>" . sprintf("%1.02f", $columnTotals[$x]) . "</b></td>"; &nbsp;} &nbsp; // print grand total echo "<td align=center><b>" . sprintf("%1.02f", $sumOfColumnTotals) . "</b></td>"; // clean up resultsets mysql_free_result($result); mysql_free_result($result2); mysql_free_result($result3); ?> </tr> </table> </td> </tr> <!-- end projects vs. users section --> <!-- end projects vs. users section --> <!-- spacer --> <tr> <td>&nbsp;</td> </tr> <!-- begin projects vs. tasks section --> <!-- begin projects vs. tasks section --> <tr> <td valign="top" align="left"> <b><font color="#3098C3">&nbsp;&nbsp;Projects/Tasks:</font></b> </td> </tr> <tr> <td> <!-- projects versus tasks table --> <table width=100% &nbsp;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()); &nbsp;// print tasks in top row &nbsp;while (list($tid, $tname) = mysql_fetch_row($result)) &nbsp;{ &nbsp;echo "<td valign=top align=center><font color=#D03468>$tname</font></td>"; &nbsp;} ?> <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()); &nbsp;// for each project in list... &nbsp;while (list($pid, $pname) = mysql_fetch_row($result2)) &nbsp;{ &nbsp;// go back to top of task list &nbsp;mysql_data_seek($result, 0); &nbsp;// print project name as first column &nbsp;echo "<tr>"; &nbsp;echo "<td valign=top><font color=#D03468>$pname</font></td>"; &nbsp;$rowTotal = 0; &nbsp;$count = 0; &nbsp; // get intersection of this task and this project &nbsp; while (list($tid, $tname) = mysql_fetch_row($result)) &nbsp; { &nbsp; $query3 = "SELECT SUM(hours) from log WHERE pid = '$pid' AND tid = '$tid' AND date >= '$sdate' AND date <= '$edate'"; &nbsp; $result3 = mysql_db_query($database, $query3, $connection) or die ("Error in query: $query3 . " . mysql_error()); &nbsp; list($sum) = mysql_fetch_row($result3); &nbsp; &nbsp; &nbsp; &nbsp;// correction for zero values &nbsp; &nbsp; &nbsp;if (!$sum) { $sum = 0; } &nbsp; &nbsp; // keep track of totals for later use &nbsp; $rowTotal = $rowTotal + $sum; &nbsp; $columnTotals[$count] = $columnTotals[$count] + $sum; &nbsp; $count++; &nbsp; &nbsp; // print sum &nbsp; echo "<td align=center>" . sprintf("%1.02f", $sum) . "</td>"; &nbsp; } &nbsp; &nbsp;// print row total &nbsp;echo "<td align=center><b>" . sprintf("%1.02f", $rowTotal) . "</b></td>"; &nbsp;} echo "</tr>"; ?> <tr> <? echo "<td>&nbsp;</td>"; $sumOfColumnTotals = 0; &nbsp;// print last row - column totals &nbsp;for ($x=0; $x<sizeof($columnTotals); $x++) &nbsp;{ &nbsp;$sumOfColumnTotals = $sumOfColumnTotals + $columnTotals[$x]; &nbsp;echo "<td align=center><b>" . sprintf("%1.02f", $columnTotals[$x]) . "</b></td>"; &nbsp;} // print last cell - grand total aka sum of column totals echo "<td align=center><b>" . sprintf("%1.02f", $sumOfColumnTotals) . "</b></td>"; // clean up resultsets mysql_free_result($result); mysql_free_result($result2); mysql_free_result($result3); ?> </tr> </table> </td> </tr> <!-- end projects vs. tasks section --> <!-- end projects vs. tasks section --> </table> <? } // report required for a specific department else { ?> <!-- main table &nbsp;--> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <!-- title --> <tr> <td valign="top" align="left" colspan=2> <? // get name of selected project $query = "SELECT pname FROM projects WHERE pid = '$pid'"; $result = mysql_db_query($database, $query, $connection) or die ("Error in query: $query . " . mysql_error()); $row = mysql_fetch_row($result); ?> <b><font color="#3098C3">Activity report for "<? echo $row[0]; ?>" for the period <? echo fixDate($sdate); ?> to <? echo fixDate($edate); ?></font></b> </td> </tr> <!-- spacer --> <tr> <td colspan=2>&nbsp;</td> </tr> <!-- begin tasks vs. users section --> <!-- begin tasks vs. users section --> <tr> <td colspan="2" valign="top" align="left"> <b><font color="#3098C3">&nbsp;&nbsp;Tasks/Users:</font></b> </td> </tr> <tr> <td valign="top" align="left" colspan="2"> <!-- tasks 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 users 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> <? // create variables to hold row and column totals (useful later) $rowTotals = array(); $columnTotals = array(); // get list of tasks $query2 = "SELECT tid, tname FROM tasks"; $result2 = mysql_db_query($database, $query2, $connection) or die ("Error in query: $query2 . " . mysql_error()); // iterate through resultset while (list($tid, $tname) = mysql_fetch_row($result2)) { echo "<tr>"; echo "<td valign=top align=left><font color=#D03468>$tname</font></td>"; mysql_data_seek($result, 0); &nbsp; &nbsp;// for each task-user combination &nbsp;while (list($uid, $uname) = mysql_fetch_row($result)) &nbsp;{ &nbsp;// get intersection &nbsp;$query3 = "SELECT SUM(hours) from log WHERE pid = '$pid' AND tid = '$tid' AND uid = '$uid' AND date >= '$sdate' AND date <= '$edate'"; &nbsp;$result3 = mysql_db_query($database, $query3, $connection) or die ("Error in query: $query3 . " . mysql_error()); &nbsp;list($sum) = mysql_fetch_row($result3); &nbsp; &nbsp; // correction &nbsp; if (!$sum) { $sum = 0; } &nbsp; &nbsp;// keep track of totals &nbsp;$rowTotals[$tid] = $rowTotals[$tid] + $sum; &nbsp;$columnTotals[$uid] = $columnTotals[$uid] + $sum; &nbsp;// print value &nbsp;echo "<td valign=top align=center>" . sprintf("%1.02f", $sum) . "</td>"; &nbsp;} // print row total echo "<td valign=top align=center><b>" . sprintf("%1.02f", $rowTotals[$tid]) . "</b></td>"; echo "</tr>"; } ?> <tr> <td>&nbsp;</td> <? // back to top of user list mysql_data_seek($result, 0); &nbsp;// print column totals &nbsp;while (list($uid, $uname) = mysql_fetch_row($result)) &nbsp;{ &nbsp;$sumOfColumnTotals = $sumOfColumnTotals + $columnTotals[$uid]; &nbsp;echo "<td valign=top align=center><b>" . sprintf("%1.02f", $columnTotals[$uid]) . "</b></td>"; &nbsp;} // print grand total echo "<td valign=top align=center><b>" . sprintf("%1.02f", $sumOfColumnTotals) . "</b></td>"; ?> </tr> </table> </td> </tr> <!-- spacer --> <tr> <td colspan=2>&nbsp;</td> </tr> <tr> <td colspan=2 valign="top" align="left"> <b><font color="#3098C3">&nbsp;&nbsp;Tasks/Users Summary:</font></b> </td> </tr> <tr> <td valign="top" align="left"> <!-- tasks summary - these are the row totals --> <table border="0" cellspacing="2" cellpadding="5"> <tr> <td valign=top><font color=#D03468>Task</font></td> <td valign=top align=center><font color=#D03468>Hours</td> </tr> <? // iterate through task list mysql_data_seek($result2, 0); &nbsp;// for each task, get corresponding row total and print &nbsp;while (list($tid, $tname) = mysql_fetch_row($result2)) &nbsp;{ &nbsp;$sumOfRowTotals = $sumOfRowTotals + $rowTotals[$tid]; &nbsp;echo "<tr>"; &nbsp;echo "<td valign=top align=left>$tname</td>"; &nbsp;echo "<td valign=top align=center>" . sprintf("%1.02f", $rowTotals[$tid]) . "</td>"; &nbsp;echo "</tr>"; &nbsp;} ?> &nbsp; &nbsp;<tr> &nbsp;<td>&nbsp;</td> &nbsp;<td valign="top" align="center"><hr><b><? echo sprintf("%1.02f", $sumOfRowTotals); ?></b></td> &nbsp;</tr> </table> &nbsp; </td> <td valign="top" align="left"> <!-- user hours summary - these are the column totals --> <table border="0" cellspacing="2" cellpadding="5"> <tr> <td valign=top><font color=#D03468>User</font></td> <td valign=top align=center><font color=#D03468>Hours</td> </tr> <? // iterate through user list mysql_data_seek($result, 0); &nbsp;// print column totals &nbsp;while (list($uid, $uname) = mysql_fetch_row($result)) &nbsp;{ &nbsp;echo "<tr>"; &nbsp;echo "<td valign=top align=left>$uname</td>"; &nbsp;echo "<td valign=top align=center>" . sprintf("%1.02f", $columnTotals[$uid]) . "</td>"; &nbsp;echo "</tr>"; &nbsp;} ?> &nbsp; &nbsp;<tr> &nbsp;<td>&nbsp;</td> &nbsp;<td valign="top" align="center"><hr><b><? echo sprintf("%1.02f", $sumOfColumnTotals); ?></b></td> &nbsp;</tr> </table> </td> </tr> </table> <? // clean up resultsets mysql_free_result($result); mysql_free_result($result2); mysql_free_result($result3); } // main "if" loop done - now clean up and print page footer mysql_close($connection); ?> <? include("footer.inc.php"); ?> </body> </html>
__________________


Reply With Quote