2011-11-01 27 views
1

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

$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>"; 
+0

我或许会去jQuery的方式。它简单而干净。 http://plugins.jquery.com/project/pagination –

+1

@Nikhil:为什么??!你正在创建对Javascript的依赖,插件会比较慢,他可能会把所有的数据发送到页面 - 这很慢,很可能。停止向客户推送不必推到那里的东西! – 2011-11-01 14:58:40

回答

1

这里就是你需要做的事。

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

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

如果你想要第二个到最后一个按钮,只要进入页=总页数 - 1等

你可能要考虑Zend_Paginator - 你不需要使用整个的Zend框架使用它的各个部分,它的设计可以将它拉开。

+0

当它返回一个众所周知的错误404页面时,请更新Zend_Paginator的链接... – jagb

1

他们的分页脚本的完整代码是免费的@Stranger Studios有一个phpPerl脚本可供下载。

代码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

有很多关于分页的问题,我希望这是对用户有帮助谁需要一个分页脚本..