Jump to content

PHP running totals


bululu
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi PHP Frieks, I have a problem that I wish you guys will solve (thanks in advance). I am pulling data from mysql (the Payment Column) and want to do running totals using PHP in column called Runnng Totals. As can be seen we have  I prefer a PHP solution to the running totals as opposed to doing te totals in a MyQL query.

ID   Payment   Running Totals
1    250       250
2    300       550
5    220       770
7    100       870

I have not yet come up with any code worthy posting as I am still fiddling with some code, and will post my efforts as soon I can come up with something sensible. Any help will be deeply appreciated.

Link to comment
Share on other sites

  • Solution

As you output the Payment value you add it to a $total variable

$total += $row['Payment']; // add current payment to total

and then echo the $total

 

Example

$result = msyql_query('SELECT id, Payment FROM payment_table');

$total = 0; // init total to zero
while($row = mysql_fetch_assoc($result))
{
   $total += $row['Payment'];

   echo $row['id'] . ' - ' . $row['Payment'] . ' - ' . $total . '<br />';
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.