7

I have a pagination script with PHP. When page records are some hundreds, the pagination result is too big. How I can limit the page numbers/links?

Example: < 1 | 2 ... 37 | 38 | 39 | 40 | 41 | 42 ... 82 | 83 >

This is my PHP script

<?php
$ppp = 10;
$rows = mysql_num_rows($query);

$nmpages = ceil($rows/$ppp);

// if current page is not 1, draw PREVIOUS link
if ($pg > 1 && $nmpages != 0) {
  echo "<a href=\"?pg=".($pg-1)."\">&lt;</a> ";
}

For($i = 1 ; $i <= $nmpages ; $i++) {
    If($i == $pg) {
      echo "<a href=\"#\" class=\"selected\"><b>".$i."</b></a> ";
   } else {
      echo "<a href=\"?pg=".$i."\">".$i."</a> ";
    }
}
// if current page less than max pages, draw NEXT link
if ($pg < $nmpages && $nmpages != 0) {
  echo "<a href=\"?pg=".($pg+1)."\">&gt;</a>";
}
?>

Do you have an ideas how I can do this with the specific PHP script that I have?

Rikesh
  • 26,156
  • 14
  • 79
  • 87
user3122748
  • 91
  • 1
  • 1
  • 7
  • possible duplicate of [Smart pagination algorithm](http://stackoverflow.com/questions/163809/smart-pagination-algorithm) –  Feb 25 '15 at 10:48
  • Refer this [question][1] [1]: http://stackoverflow.com/questions/15241847/pagination-to-show-max-value-and-limit-the-rest/15242313#15242313 possibly that will be your answer – varad mayee Feb 25 '15 at 10:55
  • 1
    You will need to know two things: What page you are on and how many pages there are in total. Divide and conquer; split the problem up into smaller problems, and it will be much easier to solve. – Sverri M. Olsen Feb 25 '15 at 10:58

5 Answers5

11

Try this :

    <?php
        $link = "";
 $page = $_GET['pg']; // your current page
 // $pages=20; // Total number of pages

  $limit=5  ; // May be what you are looking for

    if ($pages >=1 && $page <= $pages)
    {
        $counter = 1;
        $link = "";
        if ($page > ($limit/2))
           { $link .= "<a href=\"?page=1\">1 </a> ... ";}
        for ($x=$page; $x<=$pages;$x++)
        {

            if($counter < $limit)
                $link .= "<a href=\"?page=" .$x."\">".$x." </a>";

            $counter++;
        }
        if ($page < $pages - ($limit/2))
         { $link .= "... " . "<a href=\"?page=" .$pages."\">".$pages." </a>"; }
    }

    echo $link;
?>

OUTPUT :

//At page=1
1 2 3 4 ... 20 

//At page=12
1 ... 12 13 14 15 ... 20 

//At page=18
1 ... 18 19 20 
Makesh
  • 1,236
  • 1
  • 11
  • 25
11

An improvement or rather re-write based on @Makesh's code.

function get_pagination_links($current_page, $total_pages, $url)
{
    $links = "";
    if ($total_pages >= 1 && $current_page <= $total_pages) {
        $links .= "<a href=\"{$url}?page=1\">1</a>";
        $i = max(2, $current_page - 5);
        if ($i > 2)
            $links .= " ... ";
        for (; $i < min($current_page + 6, $total_pages); $i++) {
            $links .= "<a href=\"{$url}?page={$i}\">{$i}</a>";
        }
        if ($i != $total_pages)
            $links .= " ... ";
        $links .= "<a href=\"{$url}?page={$total_pages}\">{$total_pages}</a>";
    }
    return $links;
}

OUTPUT:

page = 1
1 2 3 4 5 6 ... 20 

page = 10
1 ... 5 6 7 8 9 10 11 12 13 14 15 ... 20 

page = 19
1 ... 14 15 16 17 18 19 20 
Maziyar Mk
  • 1,179
  • 14
  • 17
2

The answer for this question was basically to visit a page about Digg style pagination which includes code samples.

So that's the answer, but this question is basically a duplicate.

Community
  • 1
  • 1
M1ke
  • 6,166
  • 4
  • 32
  • 50
  • The Digg style pagination is a very good example to do this and it's easy to use in custom pagination scripts as it's very readable code.. – jagb Jul 25 '16 at 20:37
1

Try to make a page bracket for example 10 less and 10 more than the actual page, change for example the for statement for this:

For($i = $pg-10 ; $i <= $pg+10 ; $i++)
Jmunoz Dev
  • 461
  • 5
  • 10
0

I wanted to get an array of numbers by the current page and total page. So this is what I came up with. I hope this helps others -

Caution - this work if total page is more than 10. I felt if the total page is less than 10 then just show it in a single for loop.

function getSmartPageNumbers($currentPage, $totalPage)
{
    $pageNumbers = [];
    $diff = 2;
    
    $firstChunk = [1, 2, 3];
    $lastChunk = [$totalPage - 2, $totalPage - 1, $totalPage];
    
    if ($currentPage < $totalPage) {
        $loopStartAt = $currentPage - $diff;
        if ($loopStartAt < 1) {
            $loopStartAt = 1;
        }
        
        $loopEndAt = $loopStartAt + ($diff * 2);
        if ($loopEndAt > $totalPage) {
            $loopEndAt = $totalPage;
            $loopStartAt = $loopEndAt - ($diff * 2);
        }
        
        if (!in_array($loopStartAt, $firstChunk)) {
            foreach ($firstChunk as $i) {
                $pageNumbers[] = $i;
            }
            
            $pageNumbers[] = '.';
        }
        
        for ($i = $loopStartAt; $i <= $loopEndAt; $i++) {
            $pageNumbers[] = $i;
        }
        
        if (!in_array($loopEndAt, $lastChunk)) {
            $pageNumbers[] = '.';
            
            foreach ($lastChunk as $i) {
                $pageNumbers[] = $i;
            }
        }
    }
    
    return $pageNumbers;
}

Test:

getSmartPageNumbers(8, 20);
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => .
    [4] => 6
    [5] => 7
    [6] => 8
    [7] => 9
    [8] => 10
    [9] => .
    [10] => 18
    [11] => 19
    [12] => 20
)
getSmartPageNumbers(1, 20);
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => .
    [6] => 18
    [7] => 19
    [8] => 20
)
HADI
  • 2,829
  • 1
  • 26
  • 26