【问题标题】:PHP Pagination show first two pages and last two pagesPHP分页显示前两页和最后两页
【发布时间】:2019-12-20 23:15:56
【问题描述】:

我目前正在使用下面的 php 分页脚本。分页每页显示六个结果,带有上一个/下一个链接。我想知道是否有人可能知道我可以添加什么以便分页还显示前两页和最后两页的链接?喜欢这个示例页面中的分页:http://www.winelog.net/wines/Oregon

$data=file("data.txt");
$pages=0;
foreach($data as $temp){
    $x=explode("|",$temp);
    if($x[0] > 0){
        $pages=$pages+1;
    }
}

if($_GET['p']){
    $page=$_GET['p'];
}

if($_GET['i']){
    $index=$_GET['i'];
}

if($index == "p"){
    $page=$page-1;
}
if($index == "n"){
    $page=$page+1;
}
if($page < 1){
    $page=1;
}
if($page  >  $pages){
    $page=$pages;
}
$line=$data[$page-1];
$fields=explode("|",$line);

显示的导航:

$show=6;
echo "<li><a href='?i=p&p=$page'>&#171; PREV</li></a>";

if($page-($show/2)  >  1){
    $temp=$page-$show;
    echo "<li><a href='?p=$temp'>...</li></a>";
}

if($page-($show/2) >= 1 && $page+($show/2) <= $pages){
    $start=$page-($show/2);
    $stop=$page+($show/2);
}

if($page-($show/2) < 1){
    $start=1;
    $stop=$show;
}

if($page+($show/2) > $pages){
    $start=$pages-$show;
    $stop=$pages;
}

for($i=$start; $i<=$stop; $i++){
    if($page==$i){
        echo "<li class='active'>$i</li></a>";
    }
    else{
        echo "<li><a href='?p=$i'>$i</li></a>";
    }
}

if($page+($show/2) < $pages){
    $temp=$page+$show;
    echo "<li><a href='?p=$temp'>...</li></a>";
}
echo "<li><a href='?i=n&p=$page'>NEXT &#187;</li></a>";

【问题讨论】:

  • 我可能会采用 jquery 的方式。它简单而干净。 plugins.jquery.com/project/pagination
  • @Nikhil:为什么??!您正在创建对 Javascript 的依赖项,该插件会变慢,并且他可能必须将所有数据发送到页面 - 可能非常慢。停止向客户端推送不需要推送的内容!

标签: php pagination


【解决方案1】:

这是你需要做的。

首先计算“页面”的数量。
如果少于 10 页 - 输出所有页面。

否则: 从当前页面输出 - 5 到当前页面 + 5。
在输出之前,放一个“第一个”按钮 - page = 1
在输出之后,放一个“Last”按钮 - page = 总页数。

如果您想要倒数第二个按钮,只需转到页面 = 总页数 - 1 等。

您可能想查看Zend_Paginator - 您无需使用整个 Zend 框架即可使用其中的各个部分,它的设计目的是可以将其拆开。

【讨论】:

  • 请更新 Zend_Paginator 的链接,因为它返回一个众所周知的 ERROR 404 页面...
【解决方案2】:

他们的分页脚本的完整代码可免费获得@Stranger Studios 有一个php 和一个Perl 脚本可供下载。

代码PHP版本:

<?php
//function to return the pagination string
function getPaginationString($page = 1, $totalitems, $limit = 15, $adjacents = 1, $targetpage = "/", $pagestring = "?page=")
{       
    //defaults
    if(!$adjacents) $adjacents = 1;
    if(!$limit) $limit = 15;
    if(!$page) $page = 1;
    if(!$targetpage) $targetpage = "/";

    //other vars
    $prev = $page - 1;                                  //previous page is page - 1
    $next = $page + 1;                                  //next page is page + 1
    $lastpage = ceil($totalitems / $limit);             //lastpage is = total items / items per page, rounded up.
    $lpm1 = $lastpage - 1;                              //last page minus 1

    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<div class=\"pagination\"";
        if($margin || $padding)
        {
            $pagination .= " style=\"";
            if($margin)
                $pagination .= "margin: $margin;";
            if($padding)
                $pagination .= "padding: $padding;";
            $pagination .= "\"";
        }
        $pagination .= ">";

        //previous button
        if ($page > 1) 
            $pagination .= "<a href=\"$targetpage$pagestring$prev\">� prev</a>";
        else
            $pagination .= "<span class=\"disabled\">� prev</span>";    

        //pages 
        if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination .= "<span class=\"current\">$counter</span>";
                else
                    $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";                 
            }
        }
        elseif($lastpage >= 7 + ($adjacents * 2))   //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page < 1 + ($adjacents * 3))        
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination .= "<span class=\"current\">$counter</span>";
                    else
                        $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";                 
                }
                $pagination .= "<span class=\"elipses\">...</span>";
                $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
                $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";       
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
                $pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
                $pagination .= "<span class=\"elipses\">...</span>";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination .= "<span class=\"current\">$counter</span>";
                    else
                        $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";                 
                }
                $pagination .= "...";
                $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
                $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";       
            }
            //close to end; only hide early pages
            else
            {
                $pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
                $pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
                $pagination .= "<span class=\"elipses\">...</span>";
                for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination .= "<span class=\"current\">$counter</span>";
                    else
                        $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";                 
                }
            }
        }

        //next button
        if ($page < $counter - 1) 
            $pagination .= "<a href=\"" . $targetpage . $pagestring . $next . "\">next �</a>";
        else
            $pagination .= "<span class=\"disabled\">next �</span>";
        $pagination .= "</div>\n";
    }

    return $pagination;

}
?>

更改代码以满足您的需要并添加一些CSS

有很多关于分页的问题,希望对需要分页脚本的用户有所帮助..

【讨论】:

    【解决方案3】:

    到 winelog 网站的链接已损坏。因此,我看不到您感兴趣的样本。 我用 PHP 编写了一个分页脚本,它显示了活动页面的两个相邻页面,即

    < 1  2  3 4  5 >  // 3 being the active page
    
    < 1  2  3  4 >  // 2 being the active page
    

    代码如下:

    /* set total records */
    $total_records = 26;  // this is the total count of values in an array or query results from the database
    
    /* set records per page */
    $records_per_page = 5;
    
    /* calculate total pages */
    $total_pages = ceil($total_records / $records_per_page);
    
    /* get and set value for current page */
    if (isset($_GET['page'])) {
        $current_page = $_GET['page'];
    } else {
        $current_page = 1;
    }
    
    /* calculate and set previous and next page values */
    $previous = $current_page - 1;
    $next = $current_page + 1;
    
    /* set start page value */
    $start_page = 1;
    
    /* set number of pages to display to the left */
    /* maximum value should be 4 */
    $pages_to_left = 2;
    
    /* set number of pages to display to the right */
    /* maximum value should be 4 */
    $pages_to_right = 2;
    
    /* show previous pages to the left */
    if ($current_page <= $total_pages && $current_page > $start_page + $pages_to_left) {
        $start_page = $current_page - $pages_to_left;
    }
    
    /* show next pages to the right */
    if ($current_page <= $total_pages && $current_page > $start_page - $pages_to_right) {
        $end_page = $current_page + $pages_to_right;
        if ($current_page == $total_pages || $current_page + 1 == $total_pages || $current_page + 2 == $total_pages || $current_page + 3 == $total_pages) {
            $end_page = $total_pages;
        }
    } else {
        $end_page = $total_pages;
    }
    
    /* show previous button */
    if ($current_page > 1) {
        echo '<a href="?page='.$previous.'">&laquo;</a>';
        echo "&nbsp;&nbsp;";
    }
    
    /* display pages */
    for ($page = $start_page; $page <= $end_page; $page++) {
        echo '<a href="?page='.$page.'">'.$page.'</a>';
        echo "&nbsp;&nbsp;";
    }
    
    /* show last page button */
    if ($end_page + $pages_to_right <= $total_pages || $end_page != $total_pages) {
        echo '<a href="?page='.$total_pages.'">&hellip;' . $total_pages . '</a>';
    }
    
    /* show next button */
    if ($current_page < $total_pages) {
        echo "&nbsp;&nbsp;";
        echo '<a href="?page='.$next.'">&raquo;</a>';
    }
    

    这是脚本的 pastebin 链接:https://pastebin.com/espTvimy

    请检查一下,让我知道它是否适合您。

    【讨论】:

      猜你喜欢
      • 2021-02-05
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 2023-03-24
      相关资源
      最近更新 更多