user3122748
user3122748

Reputation: 91

Limit pages numbers on PHP pagination

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?

Upvotes: 7

Views: 19962

Answers (5)

Hadi
Hadi

Reputation: 2905

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
)

Upvotes: 0

Maziyar Mk
Maziyar Mk

Reputation: 1328

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 

Upvotes: 13

Makesh
Makesh

Reputation: 1234

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 

Upvotes: 11

Jmunoz Dev
Jmunoz Dev

Reputation: 461

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++)

Upvotes: 1

M1ke
M1ke

Reputation: 6406

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.

Upvotes: 2

Related Questions