3

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Alfred
  • 21,058
  • 61
  • 167
  • 249
  • Isn't the total results in a specific page controlled by $limit? That's my understanding of what your wrote. Maybe $limit has another meaning besides "limit entries to this amount per page"? – SRM Jun 16 '11 at 17:05

5 Answers5

7

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);
Vinicius Kamakura
  • 7,665
  • 1
  • 29
  • 43
2

R = Number of rows on a given page, P, where P starts at 1.
T = Total rows
L = Rows per page

R = T - ((P-1) * L)

Then just add a check afterwards to set R to 0 if R < 0.

Code:

function get_num_rows_on_page($page, $total_rows, $per_page) {
    $on_page = $total_rows - ( ($page-1) * $per_page );
    return $on_page < 0 ? 0 : $on_page;
}
simshaun
  • 21,263
  • 1
  • 57
  • 73
1

Max pages: Total files, divided by the limit. If the remainder is greater than 0, add one.

$maxpages = $totalfiles/$limit;
if ($totalfiles % $limit > 0) $maxpages++;

Number on current page: if Page number less than Max page, there are limit results. If Page number is the Max page, there are (Remainder of Total Files divided by limit) results if that remainder is greater than 0, otherwise limit.

$results = $limit;
$rem = $totalfiles % limit;
if ($pagenumber == $maxpages && $rem > 0) $results = $rem;
Fosco
  • 38,138
  • 7
  • 87
  • 101
0

If you want to distribute the results evenly in the maximum number of pages, which is 3 as you have suggested, you can use:

$results_per_page = ($totalfiles/$maxpages);

Otherwise, you already have the number of results per page calculated on your $limit variable.

Shef
  • 44,808
  • 15
  • 79
  • 90
0

Try this:

function getPageSize($total, $maxpages, $pagenumber){

    $itemsperpage=intval($total/$maxpages);
    if($pagenumber == $maxpages){
        $itemslastpage=abs($total-($itemsperpage*$maxpages));
        return $itemslastpage;
    }else{
        return $itemsperpage;
    }
}

// should print '5'
echo getPageSize(29,6,6) . "\n";

// should print '7'
echo getPageSize(14,2,1) . "\n";

Note that $limit is not needed as it is only used to control results from database.

AJ.
  • 27,586
  • 18
  • 84
  • 94