PHP分页显示前两页和后两页 [英] PHP Pagination show first two pages and last two pages

查看:86
本文介绍了PHP分页显示前两页和后两页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently using the php pagination script below. The pagination displays six results per page with previous/next links. I was wondering if anyone might know what I could add so that the pagination also displays links to the first two pages and the last two pages? Like the pagination in this example page: 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>";

推荐答案

这是您需要做的.

首先获取存在的页面"数.
如果少于10页,请输出所有页面.

First get a count of the number of 'pages' that there are.
If there are less than, say, 10 pages - output all the pages.

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

Otherwise: Output from current page - 5 to current page + 5.
Before the output, put a 'First' button - page = 1
After the output, put a 'Last' button - page = total number of pages.

如果要倒数第二个按钮,只需转到page =总页数-1等.

If you want a second-to-last button, just go page = total number of pages - 1, etc.

您可能想研究 Zend_Paginator -您不需要需要使用整个Zend Framework来使用它的各个部分,它的设计目的是可以将其拉开.

You might want to look into Zend_Paginator - You don't need to use the whole Zend Framework to use individual parts of it, it's designed so it can be pulled apart.

这篇关于PHP分页显示前两页和后两页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆