badgermaster Posted May 12, 2007 Share Posted May 12, 2007 Ok, so I am writing a program to log hours worked on a project. There are clock-in times, and clock out times, then I used a php operator to subtract the two and find the hours. This works fine. BUT... I have been searching for a way to then add all of these hours together and get a total hours. Here is the code for the table: echo "<table border='1'><tr><th>Date</th><th>Time in</th><th>Time out</th><th>Hours</th><th>Notes</th>"; $query3 = mysql_db_query($db, "SELECT * FROM times WHERE clientID='$ID' ORDER BY date"); while($result = mysql_fetch_array($query3)) { $date= $result["date"]; $IN = $result["timeIN"]; $OUT = $result["timeOUT"]; $notes = $result["notes"]; $hours = $OUT-$IN; echo "<tr><td>$date</td>"; echo "<td>$IN</td><td>$OUT</td>"; echo "<td>$hours</td>"; echo "<td>$notes</td></tr>"; } let me know of either a way to get a total hours from here, or a better way to get the same results, and the total. thanks Quote Link to comment https://forums.phpfreaks.com/topic/51109-solved-help-totaling-a-column-of-numbers-from-a-result/ Share on other sites More sharing options...
MadTechie Posted May 12, 2007 Share Posted May 12, 2007 try this $query3 = mysql_db_query($db, "SELECT SUM(timeOUT-timeIN) as dayhours, * FROM times WHERE clientID='$ID' GROUP BY date"); Quote Link to comment https://forums.phpfreaks.com/topic/51109-solved-help-totaling-a-column-of-numbers-from-a-result/#findComment-251591 Share on other sites More sharing options...
Barand Posted May 12, 2007 Share Posted May 12, 2007 Adjusting the code you already have <?php echo "<table border='1'><tr><th>Date</th><th>Time in</th><th>Time out</th><th>Hours</th><th>Notes</th>"; $query3 = mysql_db_query($db, "SELECT * FROM times WHERE clientID='$ID' ORDER BY date"); $total_hours = 0; while($result = mysql_fetch_array($query3)) { $date= $result["date"]; $IN = $result["timeIN"]; $OUT = $result["timeOUT"]; $notes = $result["notes"]; $hours = $OUT-$IN; echo "<tr><td>$date</td>"; echo "<td>$IN</td><td>$OUT</td>"; echo "<td>$hours</td>"; echo "<td>$notes</td></tr>"; $total_hours += $hours; } echo "Total hours : $total_hours"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/51109-solved-help-totaling-a-column-of-numbers-from-a-result/#findComment-251607 Share on other sites More sharing options...
badgermaster Posted May 13, 2007 Author Share Posted May 13, 2007 Thanks so much, the second one worked perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/51109-solved-help-totaling-a-column-of-numbers-from-a-result/#findComment-252179 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.