Welcome to WebmasterWorld Guest from 54.167.155.147

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to Put Totals At The End Of A Summary

PHP total the summary

   
11:14 am on May 19, 2012 (gmt 0)



Hi
This is my first post so I hope the details given are adequate.

Can any one point in the right direction.

I am running a query to select summary data and would like to total that summary data at the end of the column.
What is the best way to do this?


result = mysql_query("SELECT nomcode,sum(debit),sum(credit) FROM nomledg


echo "<tr><td align=right>";
echo $row['nomcode'];
echo "</td><td align=right>";
echo number_format($row['sum(debit)'],2);
echo "</td> <td align=right>";
echo number_format($row['sum(credit)'],2);

echo "</td><td align=right>";
echo number_format($total=($row['sum(debit)']- $row['sum(credit)']),2)
echo "</td></tr>";
9:00 pm on May 19, 2012 (gmt 0)

5+ Year Member



Hi. Welcome aboard! There are a few different ways to do what you're wanting. One in which is like you're doing now. Is there an error with what you have now? Is what you're doing now not resulting how you need or are you just looking for "other ways" of accomplishing the same results?
10:58 am on May 20, 2012 (gmt 0)



Hi

Currently what I have shown is working fine, giving me four columns in a table.

What I would like to achieve is totals at the bottom of each of the summed columns.

At present I have a sum debit, sum credit, and a sum total,
I would appreciate some guidance on how to get totals of these summed columns and present them at the bottom of there respective columns.



Regards

Noel
10:55 pm on Jun 1, 2012 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You need to initialize some variables to store the total values and then after your loop is complete and you have build the tables rows you write out one last table row of totals. Pseudocode ...


$debit = 0;
$credit = 0;
$total = 0;
while|for|foreach (<your loop that fetches and loads each MySQL row result here>) {
// MySQL get information ...
$debit += $row['sum(debit)'];
$credit += $row['sum(credit)'];
$total += $row['sum(debit)']- $row['sum(credit)'];
}