Welcome to WebmasterWorld Guest from

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)

New User

5+ Year Member

joined:May 19, 2012
posts: 2
votes: 0

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)

Junior Member

10+ Year Member

joined:June 6, 2006
votes: 2

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)

New User

5+ Year Member

joined:May 19, 2012
posts: 2
votes: 0


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.


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


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

joined:July 31, 2003
votes: 2

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)'];

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members