homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

How to Put Totals At The End Of A Summary
PHP total the summary

Msg#: 4455290 posted 11:14 am on May 19, 2012 (gmt 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>";



5+ Year Member

Msg#: 4455290 posted 9:00 pm on May 19, 2012 (gmt 0)

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?


Msg#: 4455290 posted 10:58 am on May 20, 2012 (gmt 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.




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

Msg#: 4455290 posted 10:55 pm on Jun 1, 2012 (gmt 0)

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved