精美漂亮的php分页类代码


Posted in PHP onApril 02, 2013

    这是一款简单,方便,功能齐全的分页类,可以根据自己的需要更改CSS样式文件以实现分页颜色的控制,利用php分页类,可以省去自己很多时间,只需要在分页的地方嵌入即可,下面看下使用方法:

    1,在head里包含pager.css

    <link href="pager.css" type="text/css" rel="stylesheet" />

    2,在分页处进行类的实例化:
<?php    
     include "pager.class.php";    
  $CurrentPage=isset($_GET['page'])?$_GET['page']:1;    
  //die($CurrentPage);    
     $myPage=new pager(1300,intval($CurrentPage));    
      $pageStr= $myPage->GetPagerContent();    
  //echo $pageStr;    
     $myPage=new pager(90,intval($CurrentPage));     
     $pageStr= $myPage->GetPagerContent();    
  echo $pageStr;    
    ?>  

  3,读取mysql数据库和分页计算文件需要自己写出来。例如:

    $info=mysql_query("select * from member order by id desc limit $offset,$info_num");  这样才能调到 mysql 数据库和当前页码等。

    下面是一共包含的三个文件:(1) index.php:调用分页类,显示分页 (2) pager.class.php:php分页类 (3)pager.css:css样式美化文件

    (1) index.php:

<head>    
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
<title>----分页演示-----</title>    
<link href="pager.css" type="text/css" rel="stylesheet" />    
</head>    
<body>    
    <?php    
     include "pager.class.php";    
     $CurrentPage=isset($_GET['page'])?$_GET['page']:1;    
     //die($CurrentPage);    
     $myPage=new pager(1300,intval($CurrentPage));    
      $pageStr= $myPage->GetPagerContent();    
     //echo $pageStr;    
     $myPage=new pager(90,intval($CurrentPage));     
     $pageStr= $myPage->GetPagerContent();    
     echo $pageStr;    
    ?>    
</body>    
</html>  

(2) pager.class.php:

<?php    
/*   
 * PHP分页类   
 * @package Page   
 * @Created 2013-03-27   
 * @Modify  2013-03-27   
 * @link http://www.60ie.net   
 * Example:   
       $myPage=new Pager(1300,intval($CurrentPage));   
       $pageStr= $myPage->GetPagerContent();   
       echo $pageStr;   
 */   
class Pager {    
    private $pageSize = 10;    
    private $pageIndex;    
    private $totalNum;        private $totalPagesCount;    
    private $pageUrl;    
    private static $_instance;    
    public function __construct($p_totalNum, $p_pageIndex, $p_pageSize = 10,$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;    
    }    
   
   
/**   
 * 获得实例   
 * @return     
 */   
//  static public function getInstance() {    
//      if (is_null ( self::$_instance )) {    
//          self::$_instance = new pager ();    
//      }    
//      return self::$_instance;    
//  }    
   
}    
?>  

    (3) pager.css:

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;}  

转载:http://60ie.net/article/8/365.html

PHP 相关文章推荐
php include,include_once,require,require_once
Sep 05 PHP
php目录管理函数小结
Sep 10 PHP
php变量范围介绍
Oct 15 PHP
PHP中实现生成静态文件的方法缓解服务器压力
Jan 07 PHP
PHP CURL 多线程操作代码实例
May 13 PHP
PHP生成各种常见验证码和Ajax验证过程
Jan 10 PHP
php 如何获取文件的后缀名
Jun 05 PHP
Laravel中间件实现原理详解
Oct 09 PHP
Yii2――使用数据库操作汇总(增删查改、事务)
Dec 19 PHP
PHP实现文件上传功能实例代码
May 18 PHP
PHP实现生成推广海报的方法详解
Mar 14 PHP
thinkphp框架实现路由重定义简化url访问地址的方法分析
Apr 04 PHP
php更新mysql后获取影响的行数发生异常解决方法
Mar 28 #PHP
php页面跳转代码 输入网址跳转到你定义的页面
Mar 28 #PHP
PHP 数组和字符串互相转换实现方法
Mar 26 #PHP
php中将html中的br换行符转换为文本输入中的换行符
Mar 26 #PHP
PHP字符过滤函数去除字符串最后一个逗号(rtrim)
Mar 26 #PHP
自定义php类(查找/修改)xml文档
Mar 26 #PHP
php中DOMElement操作xml文档实例演示
Mar 26 #PHP
You might like
PHP4和PHP5共存于一系统
2006/11/17 PHP
mac下Apache + MySql + PHP搭建网站开发环境
2014/06/02 PHP
php实现网站文件批量压缩下载功能
2015/10/28 PHP
教你php如何实现验证码
2016/01/20 PHP
php远程下载类分享
2016/04/13 PHP
Yii中的cookie的发送和读取
2016/07/27 PHP
Jquery Ajax的Get方式时需要注意URL地方
2011/04/07 Javascript
jQuery去掉字符串起始和结尾的空格(多种方法实现)
2013/04/01 Javascript
js全选实现和判断是否有复选框选中的方法
2015/02/17 Javascript
详解JavaScript函数
2015/12/01 Javascript
JS基于MSClass和setInterval实现ajax定时采集信息并滚动显示的方法
2016/04/18 Javascript
jQuery使用serialize()表单序列化时出现中文乱码问题的解决办法
2016/07/27 Javascript
canvas的神奇用法
2017/02/03 Javascript
Vue.js实现微信过渡动画左右切换效果
2017/06/13 Javascript
JS实现仿UC浏览器前进后退效果的实例代码
2017/07/17 Javascript
EasyUI实现下拉框多选功能
2017/11/07 Javascript
通过vue提供的keep-alive减少对服务器的请求次数
2018/04/01 Javascript
vue中defineProperty和Proxy的区别详解
2020/11/30 Vue.js
Python发送form-data请求及拼接form-data内容的方法
2016/03/05 Python
python中函数总结之装饰器闭包详解
2016/06/12 Python
Python3最长回文子串算法示例
2019/03/04 Python
python基于itchat模块实现微信防撤回
2019/04/29 Python
Python collections模块使用方法详解
2019/08/28 Python
python3.7添加dlib模块的方法
2020/07/01 Python
python实现邮件循环自动发件功能
2020/09/11 Python
css3.0 图形构成实例练习一
2013/03/19 HTML / CSS
使用CSS3来实现滚动视差效果的教程
2015/08/24 HTML / CSS
美国名牌太阳镜折扣网站:Eyedictive
2017/05/15 全球购物
Lookfantastic台湾:英国彩妆美发保养购物网
2018/03/26 全球购物
学生思想表现的评语
2014/01/30 职场文书
生日宴会主持词
2014/03/20 职场文书
三字经教学反思
2014/04/26 职场文书
人事行政经理岗位职责
2014/06/18 职场文书
终止或解除劳动合同及劳动关系的证明书
2014/10/06 职场文书
pytorch实现加载保存查看checkpoint文件
2022/07/15 Python
React更新渲染原理深入分析
2022/12/24 Javascript