在分页模式下计算表中的总记录数

I have a project with a table (figure A) and display all the records to the web browser with pagination something like this:

enter image description here

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

SQL Fiddle Example

With Limit

SQL Fiddle Example