PHP分页类代码

来源:互联网 发布:java 静态代理原理 编辑:程序博客网 时间:2024/04/29 02:47


参考:http://www.jb51.net/article/35229.htm

亲测可用,是一款比较完美的分页类。

测试运行结果裁图如下:



直接上代码:

pager.php

<?php   class Pager {       private $pageSize = 5;       private $pageIndex;       private $totalNum;       private $totalPagesCount;       private $pageUrl;       private static $_instance;    public function __construct($p_totalNum, $p_pageIndex, $p_pageSize = 5,$p_initNum=3,$p_initMaxNum=5) {           if (!isset ( $p_totalNum ) || !isset($p_pageIndex)) {               die ( "pager initial error" );           }           $this->totalNum = $p_totalNum;           $this->pageIndex = $p_pageIndex;           $this->pageSize = $p_pageSize;           $this->initNum=$p_initNum;           $this->initMaxNum=$p_initMaxNum;           $this->totalPagesCount= ceil($p_totalNum / $p_pageSize);           $this->pageUrl=$this->_getPageUrl();            $this->_initPagerLegal();       }            /**      * 获取去除page部分的当前URL字符串      *      * @return String URL字符串      */    private function _getPageUrl() {           $CurrentUrl = $_SERVER["REQUEST_URI"];           $arrUrl     = parse_url($CurrentUrl);           $urlQuery   = $arrUrl["query"];           if($urlQuery){               $urlQuery  = ereg_replace("(^|&)page=" . $this->pageIndex, "", $urlQuery);               $CurrentUrl = str_replace($arrUrl["query"], $urlQuery, $CurrentUrl);               if($urlQuery){                    $CurrentUrl.="&page";               }               else $CurrentUrl.="page";           } else {               $CurrentUrl.="?page";           }       return $CurrentUrl;     }     /*     *设置页面参数合法性     *@return void    */    private function _initPagerLegal()     {         if((!is_numeric($this->pageIndex)) ||  $this->pageIndex<1)         {             $this->pageIndex=1;         }elseif($this->pageIndex > $this->totalPagesCount)         {             $this->pageIndex=$this->totalPagesCount;         }              }   //$this->pageUrl}={$i}   //{$this->CurrentUrl}={$this->TotalPages}       public function GetPagerContent() {           $str = "<div class=\"Pagination\">";           //首页 上一页           if($this->pageIndex==1)           {               $str .="<a href='javascript:void(0)' class='tips' title='首页'>首页</a> "."\n";               $str .="<a href='javascript:void(0)' class='tips' title='上一页'>上一页</a> "."\n"."\n";           }else          {               $str .="<a href='{$this->pageUrl}=1' class='tips' title='首页'>首页</a> "."\n";                       $str .="<a href='{$this->pageUrl}=".($this->pageIndex-1)."' class='tips' title='上一页'>上一页</a> "."\n"."\n";           }                      /*          除首末后 页面分页逻辑          */           //10页(含)以下            $currnt="";            if($this->totalPagesCount<=10)            {               for($i=1;$i<=$this->totalPagesCount;$i++)               {                          if($i==$this->pageIndex)                          {    $currnt=" class='current'";}                          else                         {    $currnt="";    }                           $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;               }            }else                                //10页以上            {   if($this->pageIndex<3)  //当前页小于3                {                        for($i=1;$i<=3;$i++)                        {                            if($i==$this->pageIndex)                              {    $currnt=" class='current'";}                            else                           {    $currnt="";    }                           $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;                        }                        $str.="<span class=\"dot\">……</span>"."\n";                    for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1                    {                         $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;                    }                }elseif($this->pageIndex<=5)   //   5 >= 当前页 >= 3                {                    for($i=1;$i<=($this->pageIndex+1);$i++)                    {                         if($i==$this->pageIndex)                          {    $currnt=" class='current'";}                          else                         {    $currnt="";    }                           $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;                    }                    $str.="<span class=\"dot\">……</span>"."\n";                    for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1                    {                         $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;                    }                }elseif(5<$this->pageIndex  &&  $this->pageIndex<=$this->totalPagesCount-5 )             //当前页大于5,同时小于总页数-5                {                    for($i=1;$i<=3;$i++)                    {                        $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;                    }                     $str.="<span class=\"dot\">……</span>";                                 for($i=$this->pageIndex-1 ;$i<=$this->pageIndex+1 && $i<=$this->totalPagesCount-5+1;$i++)                    {                          if($i==$this->pageIndex)                          {    $currnt=" class='current'";}                          else                         {    $currnt="";    }                           $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;                    }                    $str.="<span class=\"dot\">……</span>";                    for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)                    {                         $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;                    }                }else               {                     for($i=1;$i<=3;$i++)                    {                        $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;                    }                     $str.="<span class=\"dot\">……</span>"."\n";                     for($i=$this->totalPagesCount-5;$i<=$this->totalPagesCount;$i++)//功能1                    {                          if($i==$this->pageIndex)                          {    $currnt=" class='current'";}                          else                         {    $currnt="";    }                           $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;                    }               }                   }                                   /*          除首末后 页面分页逻辑结束          */          //下一页 末页           if($this->pageIndex==$this->totalPagesCount)           {                  $str .="\n"."<a href='javascript:void(0)' class='tips' title='下一页'>下一页</a>"."\n" ;               $str .="<a href='javascript:void(0)' class='tips' title='末页'>末页</a>"."\n";                          }else          {               $str .="\n"."<a href='{$this->pageUrl}=".($this->pageIndex+1)."' class='tips' title='下一页'>下一页</a> "."\n";               $str .="<a href='{$this->pageUrl}={$this->totalPagesCount}' class='tips' title='末页'>末页</a> "."\n" ;           }                  $str .= "</div>";           return $str;       }     }   ?>   

pager.css

/* CSS Document */body,html{ padding:0px; margin:0px; color:#333333; font-family:"宋体",Arial,Lucida,Verdana,Helvetica,sans-serif; font-size:12px; line-height:150%;}   h1,h2,h3,h4,h5,h6,ul,li,dl,dt,dd,form,img,p,label{margin:0; padding:0; border:none; list-style-type:none;}   /**前台分页样式**/  .Pagination {margin:10px 0 0;padding:5px 0;text-align:rightright; height:20px; line-height:20px; font-family:Arial, Helvetica, sans-serif,"宋体";}   .Pagination a {margin-left:2px;padding:2px 7px 2px;}   .Pagination .dot{ border:medium none; padding:4px 8px}   .Pagination a:link, .Pagination a:visited {border:1px solid #dedede;color:#696969;text-decoration:none;}   .Pagination a:hover, .Pagination a:active, .Pagination a.current:link, .Pagination a.current:visited {border:1px solid #dedede;color:#fff; background-color:#ff6600; background-image:none; border:#ff6600 solid 1px;}   .Pagination .selectBar{ border:#dedede solid 1px; font-size:12px; width:95px; height:21px; line-height:21px; margin-left:10px; display:inline}   .Pagination a.tips{_padding:4px 7px 1px;}   

test.php

<!DOCTYPE HTML><html><head>   <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />   <title>分页类测试</title>   <link href="pager.css" type="text/css" rel="stylesheet" />   </head>   <body>  <?php    //header('content-type:text/html;charset=utf-8');require_once '../class/mysqlfunction.php';require_once '../class/mysqlconfig.php';$db = new mysql();$link = $db->connect2();//var_dump($link);$table = "***";$num = $db->totalnum($table);//echo $num;  include "pager.php";    $CurrentPage=isset($_GET['page'])?$_GET['page']:1; $pageSize = 1;  $myPage=new pager($num,intval($CurrentPage),$pageSize);    $pageStr= $myPage->GetPagerContent(); echo $pageStr;  //显示分页后表单的内容 $offset = ($CurrentPage - 1) * $pageSize; $info_num = $pageSize; $info_sql = "select * from *** order by id desc limit $offset,$info_num"; $rows = $db->fetchAll($info_sql); foreach($rows as $key=>$val){ echo $val['title']."<br>".$val['case_description']."<br>"; }    ?><?php $link = $db->close($link);?></body>   </html>  




0 0