Running total

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PhpDog
Forum Commoner
Posts: 58
Joined: Mon Jan 14, 2008 10:23 am

Running total

Post by PhpDog »

I am returning 'pages' of data from a ms sql database. The sql server handles the paging, and I now need to add a 'balance' (running total) to the data displayed.

Let's assume my data is as follows:

Amount Balance
20,000 20,000
30,000 50,000
-5.02 49,994.98
93.27 50,088.25
8,512.47 58,600.72

If I return two records per page, page two will show

Amount Balance
-5.02 49,994.98
93.27 50,088.25

Does anyone here have an idea of how cto handle the running total in PHP?
I can create a running total from the first page (0 + 20,000 + 30,000 etc) and pass the total to the next page for it to add it's contents to. But handling the running total when stepping backward through the pages has me stumped right now.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Running total

Post by Christopher »

The simplest might be to just to two queries. Get the totals by summing all the records up to the last record on the current page with "SELECT SUM(a), SUM(b) FROM foo LIMIT 1,4". Then get the records for the current page with "SELECT a,b FROM foo LIMIT 3,2". This example uses two records per page and show the second page.
(#10850)
PhpDog
Forum Commoner
Posts: 58
Joined: Mon Jan 14, 2008 10:23 am

Re: Running total

Post by PhpDog »

MS sql does not support LIMIT, this is a luxury in MySQL. :)
However, your reply has given me an idea - many thanks.
Post Reply