I have a project with a table (figure A) and display all the records to the web browser with pagination something like this:
How can I do so that the cumulative each page of the 'Amount' counted correctly in the last page?
I've try by looping but it give me result in page 1 = 21, page 2 = 24, and so on. Of course this is wrong.
Need help. Thank You
Make a separate query for the total amount:
SELECT SUM(Amount) AS TotalAmount
FROM tablename
Then display it in the end of the pages, outside of the loop.
Well MySQL WITH ROLLUP is used for this. This adds an extra row at last and can be used with aggregate functions.
SELECT
id,
IFNULL(stock,'Total') AS `stock`,
SUM(amount) AS Total
FROM board
GROUP BY stock WITH ROLLUP
You can add limit in this query according to your requirements
With Limit