+ -
当前位置:首页 → 问答吧 → 分页类(可以对URL有多个参数的也能分页,可自定义分页类型)

分页类(可以对URL有多个参数的也能分页,可自定义分页类型)

时间:2010-01-07

来源:互联网

复制代码
  1. <?php
  2. class PageClass{
  3. /*
  4.   *20090824
  5.   *我不是稻草人(QQ335759285)
  6.   *版本:0.1.0
  7.   *说明:URL有多个参数也能分页,还能自定义分页样式
  8.   *php版本>=5.0
  9.   */
  10. //$url为当前url,一般为:$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]
  11. //$cpage为当前页
  12. //$tatolPage为总页数
  13. //$tpl为模板数组
  14. //$tpl模板中{index}表示首页 {pagelist}链接列表 {option}下拉列表框 {next}下一页 {pre}上一页 {cur}当前页 {index=首页}表示首页的链接文字为首页
  15. private $url;
  16. private $cpage;
  17. private $totalPage;
  18. private $tplid;
  19. private $tpl;
  20. function __construct($url,$cpage,$totalPage,$tplid=1){
  21.   $this->url=$url;
  22.   $this->cpage=$cpage;
  23.   $this->totalPage=$totalPage;
  24.   $this->tplid=$tplid;
  25.   $this->tpl[1]="{cur=当前页} {index=首页} {next=下一页} {pre=上一页} {end=最后页} {option}"; //中文分页
  26.   $this->tpl[2]="{index=index}{pagelist}{next=next}{pre=prd}{cur=current}{option}{end=end}"; //英文分页
  27. }
  28. function showPage(){
  29.   //显示分页
  30.   $urlOption=array();//url的后缀如:?page=1&typeid=1
  31.   $parse_url=parse_url($this->url);
  32.   $urlMain='http://'.$parse_url['path'];
  33.   if($parse_url['query']){
  34.    //url有参数
  35.    $urlArr=split('&',$parse_url['query']);
  36.    if(is_array($urlArr)){
  37.        foreach($urlArr as $key=>$value){
  38.         $c=split('=',$value);
  39.         if($c[0]==page){
  40.         }else{
  41.          array_push($urlOption,$c[0].'='.$c[1]);
  42.         }
  43.        }
  44.    }
  45.   }else{
  46.    //url没有参数
  47.    //if($this->cpage<$this->totalPage){
  48.    // array_push($urlOption,"page=2");
  49.    //}
  50.   }
  51.   if(is_array($urlOption)){
  52.     $urlOptionStr=implode('&',$urlOption);
  53.   }
  54.   $tplcontent=$this->tpl[$this->tplid];//分页模板
  55.   $showPage=$tplcontent;
  56.   //首页
  57.   if (preg_match_all('/\{index=([^}]*+)\}/', $tplcontent, $matches)){
  58.       $t_tpl=$matches[0][0]; //模板内容
  59.       $t_word=$matches[1][0]; //分页字段
  60.       $indexStr='<a href="'.$urlMain.'?page=1'.'&'.$urlOptionStr.'">'.$t_word.'</a>';
  61.       $showPage=str_replace($t_tpl,$indexStr,$showPage);
  62.   }
  63.   //当前页
  64.   if (preg_match_all('/\{cur=([^}]*+)\}/', $tplcontent, $matches)){
  65.       $t_tpl=$matches[0][0];
  66.       $t_word=$matches[1][0];
  67.       $curStr=$t_word.$this->cpage.'/'.$this->totalPage;
  68.       $showPage=str_replace($t_tpl,$curStr,$showPage);
  69.   }
  70.   //末页
  71.   if (preg_match_all('/\{end=([^}]*+)\}/', $tplcontent, $matches)){
  72.       $t_tpl=$matches[0][0];
  73.       $t_word=$matches[1][0];
  74.         $endPage='<a href="'.$urlMain.'?page='.$this->totalPage.'&'.$urlOptionStr.'">'.$t_word.'</a>';
  75.         $showPage=str_replace($t_tpl,$endPage,$showPage);
  76.     }
  77.   //上一页
  78.   if (preg_match_all('/\{pre=([^}]*+)\}/', $tplcontent, $matches)){
  79.       $t_tpl=$matches[0][0];
  80.       $t_word=$matches[1][0];
  81.         if($this->cpage!=1){
  82.                $prePage='<a href="'.$urlMain.'?page='.($this->cpage-1).'&'.$urlOptionStr.'">'.$t_word.'</a>';
  83.         }else{
  84.                $prePage=$t_word;
  85.         }
  86.         $showPage=str_replace($t_tpl,$prePage,$showPage);
  87.   }
  88.     //下一页
  89.   if (preg_match_all('/\{next=([^}]*+)\}/',$tplcontent, $matches)){
  90.       $t_tpl=$matches[0][0];
  91.       $t_word=$matches[1][0];
  92.         if($this->cpage!=$this->totalPage && $this->totalPage>1){
  93.            $nextPage=' <a href="'.$urlMain.'?page='.($this->cpage+1).'&'.$urlOptionStr.'">'.$t_word.'</a>';
  94.           }else{
  95.                $nextPage=$t_word;
  96.         }
  97.         $showPage=str_replace($t_tpl,$nextPage,$showPage);
  98.     }
  99.     //链接列表
  100.     if (preg_match("{pagelist}",$tplcontent)){
  101.         for($i=1;$i<$this->totalPage+1;$i++){
  102.             $linkPage.=' <a href="'.$urlMain.'?page='.$i.'&'.$urlOptionStr.'">'.$i.'</a>';
  103.         }
  104.         $showPage=str_replace('{pagelist}',$linkPage,$showPage);
  105.     }
  106.     //下拉框分页
  107.     if (preg_match("{option}",$tplcontent)){
  108.         $optionPage='<select onchange="javascript:window.location='."'".$urlMain."?page='+this.options[this.selectedIndex].value+"."'&$urlOptionStr'".';">';
  109.         for($i=1;$i<$this->totalPage+1;$i++){
  110.             if($i==$this->cpage){
  111.                 $optionPage.="<option selected='selected' value='$i'>第".$i."页</option>\n";
  112.             }else{
  113.                 $optionPage.="<option value='$i'>第".$i."页</option>\n";
  114.             }
  115.         }
  116.         $optionPage.='</select>';
  117.         $showPage=str_replace('{option}',$optionPage,$showPage);
  118.     }
  119.   return $showPage;
  120. }
  121. }
  122. ?>

使用示例:
$cpage=3; //当前页
$totalPage=13; //总页数
$page=new PageClass($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"],$cpage,$totalPage);
$showpage=$page->showPage();
echo $showpage;
[ 此帖被junqing124在2010-01-09 15:41重新编辑 ]

作者: junqing124   发布时间: 2010-01-07

用一下看看,

作者: tiancai1987   发布时间: 2010-01-20