只需两个参数:最傻瓜的一个通用分页类

  1. <?php 
  2. /** 
  3. ** 通用分页类。只需提供数据总数与每页显示数。 
  4. ** 无需指定URL,链接由程序生成。方便用于检索结果分页。 
  5. ** @author  : lino(luckfeng@gmail.com) 
  6. ** @site                : http://www.ypren.com 
  7. ** @version : 0.3 
  8. ** @date    :  2006/2/24 
  9. **/ 
  10. class Pager{ 
  11.         var $url
  12.         var $countall
  13.         var $page
  14.         var $thestr
  15.         var $backstr
  16.         var $nextstr
  17.         var $pg
  18.         //构造函数,实例化该类的时候自动执行该函数 
  19.         function Pager($countall,$countlist){ 
  20.                 @$this->pg=sprintf("%d",$_GET["pg"]); 
  21.                 //保证pg在未指定的情况下为从第1页开始 
  22.                 if ($this->pg==0){ 
  23.                         $this->pg=1; 
  24.                 } 
  25.                 if (!isset($this->pg)){ 
  26.                         $this->pg=1; 
  27.                 } 
  28.                 //记录数与每页显示数不能整队时,页数取余后加1 
  29.                 $this->countall = $countall
  30.                 if ($this->countall%$countlist!=0){ 
  31.                         $this->page=sprintf("%d",$this->countall/$countlist)+1; 
  32.                 } 
  33.                 else
  34.                         $this->page=$this->countall/$countlist
  35.                 } 
  36.                 
  37.                 //得到当前的URL。具体实现请看最底部的函数实体 
  38.                 $this->url = Pager::getUrl(); 
  39.                 
  40.                 //生成12345等数字形式的分页。 
  41.                 if ($this->page<=10){ 
  42.                         for ($i=1;$i<$this->page+1;$i++){ 
  43.                                 $this->thestr=$this->thestr.Pager::makepg($i,$this->pg); 
  44.                         } 
  45.                 } 
  46.                 else
  47.                         if ($this->pg<=5){ 
  48.                                 for ($i=1;$i<10;$i++){ 
  49.                                         $this->thestr=$this->thestr.Pager::makepg($i,$this->pg); 
  50.                                 } 
  51.                         } 
  52.                         else
  53.                                 if (6+$this->pg<=$this->page){ 
  54.                                         for ($i=$this->pg-4;$i<$this->pg+6;$i++){ 
  55.                                                 $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);                        
  56.                                         } 
  57.                                 } 
  58.                                 else
  59.                                         for ($i=$this->pg-4;$i<$this->page+1;$i++){ 
  60.                                                 $this->thestr=$this->thestr.Pager::makepg($i,$this->pg); 
  61.                                         } 
  62.                                         
  63.                                 } 
  64.                         } 
  65.                 } 
  66.                 //生成上页下页等文字链接 
  67.                 $this->backstr = Pager::gotoback($this->pg); 
  68.                 $this->nextstr = Pager::gotonext($this->pg,$this->page); 
  69.                 echo ($this->backstr.$this->thestr.$this->nextstr." 共".$this->countall." 条,每页".$countlist."条,分".$this->page."页"); 
  70.         } 
  71.         //生成数字分页的辅助函数 
  72.         function makepg($i,$pg){ 
  73.                 if ($i==$pg){ 
  74.                         return " <font color=red><b>".$i."</b></font>"
  75.                 } 
  76.                 else
  77.                         return " <a href=".Pager::replacepg($this->url,5,$i).">".$i."</a>"
  78.                 } 
  79.         } 
  80.         //生成上一页等信息的函数 
  81.         function gotoback($pg){ 
  82.                 if ($pg-1>0){ 
  83.                                 return $this->gotoback=" <a href=".Pager::replacepg($this->url,3,0).">首页</a> <a href=".Pager::replacepg($this->url,2,0).">上页</a>"
  84.                 } 
  85.                 else
  86.                                 return        $this->gotoback=""
  87.                 } 
  88.  
  89.         } 
  90.         //生成下一页等信息的函数 
  91.         function gotonext($pg,$page){ 
  92.                 if ($pg < $page){ 
  93.                                 return "&nbsp<a href=".Pager::replacepg($this->url,1,0).">下页</a>&nbsp<a href=".Pager::replacepg($this->url,4,0).">尾页</a>"
  94.                 } 
  95.                 else
  96.                                 return ""
  97.                 } 
  98.         } 
  99.         
  100.         //处理url中$pg的方法,用于自动生成pg=x 
  101.         function replacepg($url,$flag,$i){ 
  102.                 if ($flag == 1){ 
  103.                         $temp_pg = $this->pg; 
  104.                         return str_replace("pg=".$temp_pg,"pg=".($this->pg+1),$url); 
  105.                 } 
  106.                 else if($flag == 2) { 
  107.                         $temp_pg = $this->pg; 
  108.                         return str_replace("pg=".$temp_pg,"pg=".($this->pg-1),$url); 
  109.                 } 
  110.                 else if($flag == 3) { 
  111.                         $temp_pg = $this->pg; 
  112.                         return str_replace("pg=".$temp_pg,"pg=1",$url); 
  113.                 } 
  114.                 else if($flag == 4){ 
  115.                         $temp_pg = $this->pg; 
  116.                         return str_replace("pg=".$temp_pg,"pg=".$this->page,$url); 
  117.                 } 
  118.                 else if($flag == 5){ 
  119.                         $temp_pg = $this->pg; 
  120.                         return str_replace("pg=".$temp_pg,"pg=".$i,$url); 
  121.                 } 
  122.                 else
  123.                         return $url
  124.                 } 
  125.         } 
  126.         
  127.         
  128.         //获得当前URL的方法 
  129.         function  getUrl(){   
  130.                 $url="http://".$_SERVER["HTTP_HOST"];   
  131.                 
  132.                 if(isset($_SERVER["REQUEST_URI"])){   
  133.                         $url.=$_SERVER["REQUEST_URI"];   
  134.                 }   
  135.                 else{   
  136.                         $url.=$_SERVER["HP_SELF"];   
  137.                         if(!emptyempty($_SERVER["QUERY_STRING"])){   
  138.                                 $url.="?".$_SERVER["QUERY_STRING"];   
  139.                         }   
  140.                 }   
  141.                 //在当前的URL里加入pg=x字样 
  142.                 if (!ereg("(pg=|PG=|pG=|Pg=)"$url)){ 
  143.                         if (!strpos($url,"?")){ 
  144.                                 $url = $url."?pg=1"
  145.                         } 
  146.                         else
  147.                                 $url = $url."&pg=1"
  148.                         } 
  149.                 }                
  150.                 return  $url;   
  151.         }   
  152. ?> 


*********************************************************
这里提供自用的一个引用示例~

 

  1. $comment = new DoctorComment; 
  2.                 $pg = @$_REQUEST["pg"]; 
  3.                 $pagelist  = 10; //每页显示数 
  4.                 $limitFrom = 0;//开始limit的数 
  5.                 if (!isset($pg)){ 
  6.                         $pg=1; 
  7.                 } 
  8.                 if ($pg>1){ 
  9.                         $limitFrom = $pagelist*($pg-1); 
  10.                 } 
  11.                 else
  12.                         $limitFrom = 0; 
  13.                 } 
  14. //从mysql 数据库中取出记录集 
  15.                 $comment_arr = $comment->getDoctorCommentList($limitFrom,$pagelist); 
  16. //下面这行的功能是得到所有记录总数,也就是这段分页唯一要和数据库打交道的地方。 
  17.                 $commentNumber = $comment->countDoctorComment(); 
  18.  
  19. //在需显示处放下这行代码即可 
  20. $pager = new Pager($commentNumber,$pagelist); 

 

上一篇: 今天事还真多哦
下一篇: 有好几天没写了哈

访客评论
#1
回复 li 2006-04-09, 11:42:16
收了

谢谢
发表评论

评论内容 (必填):