Jump to content

[SOLVED] help totaling a column of numbers from a result


badgermaster

Recommended Posts

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

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";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.