Skip to content
Advertisement

Calculating number of results in a specific page using pagination

I have a search result from MySQL query or Array or something else. The result gave a variable $totalfiles say for example 25. The number of results is limited to $limit, here 12. The maximum number of pages calculated, $maxpages, will be 3.

As we consider the case, we will get 12 results for pages 1 and 2, and 1 for page 3.

What is the easiest way to predict (or calculate) the number of results in a specific page using variables $totalfiles, $limit, $maxpages and $pagenumber using PHP? I don’t think all these 4 variables are necessary for doing this.

Advertisement

Answer

The only page that is allowed to have a number that is different from $limit is the last one. Don’t forget that. And the last page will always have the remainder of the integer division

$last_page_items = $totafiles % $limit;

If $last_page_items is 0, means that you have all pages with $limit items

also,

$pages = ceil($totalfiles / $limit);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement