2

I am trying to limit the shown pagination. My site has 500+ pages, and all 500+ numbers are shown in the pagination.

I am trying to limit it like this:

Prev 1 2 3 4 5 6 Next 

My code:

$skin = new skin('site/pagination'); $pagination = '';
if ($pages >= 1 && $page <= $pages) {
    for ($x=1; $x<=$pages; $x++) {
        $TMPL['pagination'] = ($x == $page) ? '<strong><a href="/writer/'.urlencode($name).'/'.$x.'">'.$x.'</a></strong> ' : '<a href="/writer/'.urlencode($name).'/'.$x.'">'.$x.'</a> ';
        $pagination .= $skin->make();
    }
}
Adi Inbar
  • 10,985
  • 13
  • 49
  • 65
Hemang
  • 613
  • 2
  • 6
  • 12
  • 1
    Please, tell us what your variables mean. It looks like you could do it, but we need some kind of current page variable if you don't want to globally limit it by just capping out `$pages` – TheZ Jul 06 '12 at 18:22
  • 1
    See this question: – Doin Jan 07 '13 at 15:26

5 Answers5

9

pagination page number limit problem solve by chnage

for ($x=1; $x<=$pages; $x++)

to

for($x = max(1, $page - 5); $x <= min($page + 5, $pages); $x++)
Hemang
  • 613
  • 2
  • 6
  • 12
8

What do you expect this to do?:

for ($x=1; $x<=$pages; $x++)

It is going to create an entry for every page. If you don't want that, limit it how it makes sense:

for ($x=1; $x <= min(5, $pages); $x++)

Even better would be to consider the current page:

for ($x=max($curpage-5, 1); $x<=max(1, min($pages,$curpage+5)); $x++)
wallyk
  • 53,902
  • 14
  • 79
  • 135
0
//Let's say you want 3 pages on either side of your current page:

$skin = new skin('site/pagination'); $pagination = '';
$currentPage = get the current page number however you have it stored;

// set the lower bound as 3 from the current page
$fromPage = $currentPage - 3;

// bounds check that you're not calling for 0 or negative number pages
if($fromPage < 1) {
    $fromPage = 1;
}

// set the upper bound for what you want
$toPage = $fromPage + 7; // 7 is how many pages you'd like shown

// check that it doesn't exceed the maximum number of pages you have
if($toPage > $maxPages) {
    $toPage = $maxPages;
}

// iterate over your range
for ($x=$fromPage; $x<=$toPage; $x++) {
    $TMPL['pagination'] = ($x == $page) ? '<strong><a href="/writer/'.urlencode($name).'/'.$x.'">'.$x.'</a></strong> ' : '<a href="/writer/'.urlencode($name).'/'.$x.'">'.$x.'</a> ';
    $pagination .= $skin->make();
}
databyss
  • 5,538
  • 1
  • 19
  • 23
0

For large numbers of pages, consider displaying links using "logarithmic" pagination. See my answer here (PHP code included):

How to do page navigation for many, many pages? Logarithmic page navigation

Community
  • 1
  • 1
Doin
  • 6,230
  • 3
  • 31
  • 31
0

I tried the answers provided by wallyk and Hemang but they fell short for my pagination case. Their answers would sometimes display less links than the range. I had to add some max and min statements.

Here is my take in Javascript code:

var minPage = Math.max(Math.min(currentPage - (range / 2), totalPages - range), 0);
var maxPage = Math.min(Math.max(currentPage + (range / 2), range), totalPages);

The range is the number of links always displayed. The totalPages is the total number of pages to iterate over. The currentPage is the currently displayed page.

Note that my pagination index is 0 based.

Stephane
  • 8,110
  • 16
  • 85
  • 135