How to get total sum

So I need to get the total sum of items from my time sheet here.

It has an item and lets say it’s for 2 hours work at $95.00 an hour, I can get that math to work, no issue there, however,
for some reason I can not wrap my head around adding up all the items to get the total

I need
Item | Hours | Line total

A Item | 2.5 | $200.00
B Item | 4.0 | $250.00

Total Price: $450.00 <----- how do I grab the total?

In my head im thinking this is easy, simply run it threw a foreach(), but for some reason im at a loss from there LOL


<tr>    
<th>Item ID</th>    
<th>Description</th>    
<th>User</th>    
<th>Hours</th>                                        
<th>Amount</th>                                    
</tr></thead>    
<?php foreach ($data['info'] as $row):                 
$amount = $row['rate'] * $row['deductedHours']; //<-------------This is what needs to be added to get the total price for the line items
?>
<tbody>
<tr>    
<td><a href="./Item/<?php echo $row['id']; ?>">            
<?php echo $row['id']; ?></a></td>    
<td><?php echo $row['description']; ?></td>    
<td><?php echo $row['firstName'] . " " . $row['lastName']; ?></td>
<td><?php echo $row['deductedHours']; ?></td>    
<td><?php echo "$" . number_format($amount, 2); ?></td> <?php //<-------- Amount for each line item has to go here (which is working)
</tr>        
<?php endforeach; ?>
<tr>    
<td class="sub_total" colspan="2"></td>    
<td class="sub_total">Total Hours:</td>    
<td class="sub_total"><?php //echo $totalAmount; //<----------- The total amount needs to go here ?>
</td>      
<td class="sub_total">
</td>
</tr>
</tbody>
</table>

MySQL


ROUND((TIME_TO_SEC(sr.endtime) -
       TIME_TO_SEC(sr.starttime)) / 3600.0 , 2 ) AS totalHours
, ROUND((TIME_TO_SEC(sr.endtime) -
         TIME_TO_SEC(sr.starttime)) / 3600.0 , 2 ) - sr.deduction AS deductedHours


sorry for the formatting, the forums didn’t like it…

This kind of work should be done through a query, suing the sum function.


select
sum(line_totals)
from
table

However, if you insist on PHP to do it, a quick loop would work. After your first loop is complete:


$total = 0;
[COLOR=#000000][COLOR=#007700]foreach ([/COLOR][COLOR=#0000BB]$data[/COLOR][COLOR=#007700][[/COLOR][COLOR=#DD0000]'info'[/COLOR][COLOR=#007700]] as [/COLOR][COLOR=#0000BB]$row[/COLOR][/COLOR]) {
[COLOR=#000000][COLOR=#0000BB]$total = $total + $row[/COLOR][COLOR=#007700][[/COLOR][COLOR=#DD0000]'rate'[/COLOR][COLOR=#007700]] * [/COLOR][COLOR=#0000BB]$row[/COLOR][COLOR=#007700][[/COLOR][COLOR=#DD0000]'deductedHours'[/COLOR][COLOR=#007700]][/COLOR][/COLOR];
}
echo $total;

Yeah, I would rather do it via query, but not sure how lol

this is the query for the time /hours



ROUND((TIME_TO_SEC(sr.endtime) -       
TIME_TO_SEC(sr.starttime)) / 3600.0 , 2 ) AS totalHours

, ROUND((TIME_TO_SEC(sr.endtime) -         
TIME_TO_SEC(sr.starttime)) / 3600.0 , 2 ) - sr.deduction AS deductedHours

EDIT: When I run your example, it only doubles the price of the last item, not all items hmmm

EDIT 2: Got it working, lol all I needed to do was add a +=:


$total += $total + $row['rate'] * $row['deductedHours'];

Thanks for your help! much appreciated!

but only if the result should show just the sums, and not the details

if the details need to be shown, you would do the sums in php

(you could do the sums in mysql too, but then you’d need two queries that would be UNION’ed together, and then there’s a tricky matter of sorting/interleaving…)

I was thinking two separate data sets. If you get into large data sets you don’t want to be calculating a sum through PHP, I think a new mysql connection would be worth it at that point.

Sorry, used to VBA now unfortunately :frowning:

You can still wrap a sum function around anything you do in the query.


sum(ROUND((TIME_TO_SEC(sr.endtime) -        TIME_TO_SEC(sr.starttime)) / 3600.0 , 2 )) AS totalHours

it would be interesting to see how you control the interleaving of detail and total rows, if they’re coming from separate query results…



$sCriteria = "DATE RANGE HERE";

$rows = $db->query('SELECT ROUND((TIME_TO_SEC(sr.endtime) - TIME_TO_SEC(sr.starttime)) / 3600.0 , 2 ) AS totalHours FROM tablename WHERE $sCriteria');
$totals = $db->query('SELECT sum(ROUND((TIME_TO_SEC(sr.endtime) - TIME_TO_SEC(sr.starttime)) / 3600.0 , 2 )) AS totalHours FROM tablename WHERE $sCriteria');
 for each ($rows as $row) {
//echo all rows
}

echo $totals['totalHours'];

Minus the obvious syntax errors and missing logic. All one needs to do is call the same query with the aggregate functions under a new variable.

you know, i’m an idiot

i just re-read the original post, and it seems as though there aren’t any subtotals after all, just one grand total

(the tipoff was the absence of a GROUP BY clause in kyle’s queries)

hence no interleaving of details and totals, just a grand total

yeah, two separate queries is the simplest approach

Though your thought brings up a new question I’d like to find a solution for. If there were group by clauses, perhaps per month? And we wanted the subtotals below each month, what would eb the best way to handle the data at that point? I know that for a quick solution I’d probably write a loop to do exactly what I did above for each item in the group by, but I don’t know if I like that method. Youd have two SQL transactions for each item grouped in that case.

as i said, it can be done with a UNION query and a clever set of sort keys to interleave the subtotals amongst the detail lines

i wrote a tutorial/article about this once, but i can’t find it at the moment

simple enough to recreate, if you wish…

It would be interesting to see but please don’t go out of your way.