Jump to content

PHP running totals


bululu

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
https://forums.phpfreaks.com/topic/283510-php-running-totals/
Share on other sites

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
https://forums.phpfreaks.com/topic/283510-php-running-totals/#findComment-1456508
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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