限制PHP分页的页码

时间:2015-02-25 10:43:56

标签: php pagination

我有一个PHP分页脚本。当页面记录数百个时,分页结果太大。我如何限制页码/链接?

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

这是我的PHP脚本

<?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>";
}
?>

您是否了解我如何使用我拥有的特定PHP脚本执行此操作?

5 个答案:

答案 0 :(得分:5)

试试这个:

    <?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;
?>

输出:

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

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

//At page=18
1 ... 18 19 20 

答案 1 :(得分:2)

this question的答案基本上是访问有关Digg style pagination的网页,其中包含代码示例。

这就是答案,但这个问题基本上是重复的。

答案 2 :(得分:2)

基于@ Makesh代码的改进或改写。

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;
}

输出:

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 

答案 3 :(得分:1)

尝试将页面括号设置为比实际页面少10和10,例如更改for语句:

For($i = $pg-10 ; $i <= $pg+10 ; $i++)

答案 4 :(得分:0)

我想按当前页面和总页面来获取数字数组。这就是我想出的。我希望这对其他人有帮助-

警告-如果总页数超过10,则可以进行此操作。 页面小于10,然后仅在一个for循环中显示它。

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;
}

测试:

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
)