Page 1 of 1

Running total

Posted: Wed Mar 26, 2008 4:40 pm
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.

Re: Running total

Posted: Wed Mar 26, 2008 5:14 pm
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.

Re: Running total

Posted: Thu Mar 27, 2008 5:57 am
by PhpDog
MS sql does not support LIMIT, this is a luxury in MySQL. :)
However, your reply has given me an idea - many thanks.