一个简单且很好用的php分页类


Posted in PHP onOctober 26, 2013
class Page {
    // 分页栏每页显示的页数
    public $rollPage = 6;
    // 页数跳转时要带的参数
    public $parameter  ;
    // 默认列表每页显示行数
    public $listRows = 20;
    // 起始行数
    public $firstRow ;
    // 分页总页面数
    protected $totalPages  ;
    // 总行数
    protected $totalRows  ;
    // 当前页数
    protected $nowPage    ;
    // 分页的栏的总页数
    protected $coolPages   ;
    // 分页显示定制
    protected $config  = array(
     'redirect'=>false,
     'header'=>'条记录',
     'prev'=>'上一页',
     'next'=>'下一页',
     'first'=>'1',
     'last'=>'最后一页',
     'theme'=>' <div class="part1">%upPage% %first%  %prePage%  %linkPage%  %nextPage% %downPage%</div> <div class="part2">共  %totalPage% 页');
    // 默认分页变量名
    protected $varPage;
    /**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     +----------------------------------------------------------
     */
    public function __construct($totalRows,$listRows='',$parameter='') {
        $this->totalRows = $totalRows;
        $this->parameter = $parameter;
        $this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
        if(!empty($listRows)) {
            $this->listRows = intval($listRows);
        }
        $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
        $this->coolPages  = ceil($this->totalPages/$this->rollPage);
        //$_GET验证
        $this->nowPage = intval($_GET[$this->varPage]);
        $this->nowPage = $this->nowPage > 0 ? $this->nowPage : 1;
        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow = $this->listRows*($this->nowPage-1);
    }
    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name]    =   $value;
        }
    }
    /**
     +----------------------------------------------------------
     * 分页显示输出
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    public function show() {
     if(0 == $this->totalRows) return '';
     //处理参数
        $p = $this->varPage;
        $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
        $parse = parse_url($url);
        if(isset($parse['query'])) {
            parse_str($parse['query'],$params);
            unset($params[$p]);
            $url   =  $parse['path'].'?'.http_build_query($params);
        }
        /* 分页逻辑  */       
        //当总数小于显示的页码数
        if ($this->totalPages <= $this->rollPage) {
         $start = 1;
         $end   = $this->totalPages;
        }
        else{
         //
         if  ($this->nowPage <= $this->rollPage - 1) {
          $start = 1;
          $end   = $this->rollPage;
          $islast = true;
         }  
         else if ($this->nowPage > $this->totalPages - $this->rollPage + 1) {
          $start = $this->totalPages - ($this->rollPage - 1);
          $end   = $this->totalPages;
          $isfirst = true;
         }
         else{
          //浮动数
          $size = floor($this->rollPage / 2);
          $start = $this->nowPage - $size;
          $end   = $this->nowPage + $size;
          $isfirst = true;
          $islast = true;
         }
        }
        //上下翻页字符串
        $upRow   = $this->nowPage - 1;
        $downRow = $this->nowPage + 1;
        
        /* 拼装HTML */
        //< 1...     ...last >
        if ($isfirst){
         $theFirst = "<a class='firstPage' href='".$url."&".$p."=1' >".$this->config['first']."</a>";
        }
        if ($islast){
         $theEnd = "<a class='lastPage' href='".$url."&".$p."=$this->totalPages' >".$this->config['last']."</a>";
        }
        if ($upRow > 0){
         $upPage = "<a class='upPage' href='".$url."&".$p."=$upRow'>".$this->config['prev']."</a>";
        }
        if ($downRow <= $this->totalPages){
         $downPage = "<a class='downPage' href='".$url."&".$p."=$downRow'>".$this->config['next']."</a>";
        }
       if($start==3){
         $linkPage .= "<a href='".$url."&".$p."=2'>2</a>";
        }
        if($start>=4){
         $linkPage .= "<a href='".$url."&".$p."=2'>2</a> <span class='noEndClass'>...</span>";
        }
        //1 2 3 4 5
        for($i=$start;$i<=$end;$i++){
         if($i!=$this->nowPage){
          $linkPage .= " <a href='".$url."&".$p."=$i'> ".$i." </a>";
         }else{
          $linkPage .= " <span class='current'>".$i."</span>";
         }
         if($i==$end){
          if($i<$this->totalRows){
           $linkPage .= " <span class='noEndClass'>...</span>";
          }
         }
        }
        $pageStr = str_replace(
            array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%first%','%prePage%','%linkPage%','%nextPage%','%downPage%','%end%'),
            array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$theFirst,$prePage,$linkPage,$nextPage,$downPage,$theEnd),$this->config['theme']);
        //显示模式  普通false 带跳转ture
        if (!empty($this->config['redirect'])){
         $html = $pageStr;
        }else{
         //传递参数
         if($this->totalPages > 1){
          $redirect = " 到第 <form method='get' action=''><input name=".$p." type='text' class='page_text' size='3' maxlength='3' value='" . $this->nowPage ."'/> 页 <input type='submit' class='page_btn' value='确定' />";
          if ($params){
           foreach($params as $k => $v){
            $string .= "<input type='hidden' name='" . $k . "' value='" . $v . "'/>";
           }
           $redirect = $redirect . $string . '</form></div>';
          }else{
           $redirect = $redirect . '</form></div>';
          }
         }
         //生成Html字符串
         $html = $pageStr . $redirect;       
        }  
        return $html;
    }
}
PHP 相关文章推荐
php实现首页链接查询 友情链接检查的代码
Jan 05 PHP
有关php运算符的知识大全
Nov 03 PHP
基于PHP字符串的比较函数strcmp()与strcasecmp()的使用详解
May 15 PHP
高性能PHP框架Symfony2经典入门教程
Jul 08 PHP
php的XML文件解释类应用实例
Sep 22 PHP
php生成百度sitemap站点地图类函数实例
Oct 17 PHP
php读取mssql的ntext字段返回值为空的解决方法
Dec 30 PHP
php中使用sftp教程
Mar 30 PHP
浅谈PHP的反射API
Feb 26 PHP
PHP的反射机制实例详解
Mar 29 PHP
thinkphp5框架路由原理与用法详解
Feb 11 PHP
解决PhpStorm64不能启动的问题
Jun 20 PHP
PHP中VC6、VC9、TS、NTS版本的区别与用法详解
Oct 26 #PHP
zend optimizer在wamp的基础上安装图文教程
Oct 26 #PHP
php批量更改数据库表前缀实现方法
Oct 26 #PHP
PHP连接MySQL查询结果中文显示乱码解决方法
Oct 25 #PHP
学习php过程中的一些注意点的总结
Oct 25 #PHP
php模拟ping命令(php exec函数的使用方法)
Oct 25 #PHP
php导入导出excel实例
Oct 25 #PHP
You might like
PHP中最容易忘记的一些知识点总结
2013/04/28 PHP
解析PHP中如何将数组变量写入文件
2013/06/06 PHP
服务器上配置PHP运行环境教程
2015/02/12 PHP
PHP 文件锁与进程锁的使用示例
2017/08/07 PHP
PHP array_reduce()函数的应用解析
2018/10/28 PHP
Laravel框架实现的上传图片到七牛功能详解
2019/09/06 PHP
Thinkphp 框架扩展之驱动扩展实例分析
2020/04/27 PHP
js固定DIV高度,超出部分自动添加滚动条的简单方法
2013/07/10 Javascript
jQuery实现图片放大预览实现原理及代码
2013/09/12 Javascript
如何正确使用javascript 来进行我们的程序开发
2014/06/23 Javascript
jQuery on方法传递参数示例
2014/12/09 Javascript
Javascript实现可旋转的圆圈实例代码
2015/08/04 Javascript
js文本框输入内容智能提示效果
2015/12/02 Javascript
深入理解事件冒泡(Bubble)和事件捕捉(capture)
2016/05/28 Javascript
Node.js Addons翻译(C/C++扩展)
2016/06/12 Javascript
jQuery实现获取h1-h6标题元素值的方法
2017/03/06 Javascript
谈谈VUE种methods watch和compute的区别和联系
2017/08/01 Javascript
Vue 中axios配置实例详解
2018/07/27 Javascript
jQuery简单实现根据日期计算星期几的方法
2019/01/09 jQuery
详解Python的Lambda函数与排序
2016/10/25 Python
Python中staticmethod和classmethod的作用与区别
2018/10/11 Python
python实现dijkstra最短路由算法
2019/01/17 Python
Django框架实现分页显示内容的方法详解
2019/05/10 Python
在pytorch中实现只让指定变量向后传播梯度
2020/02/29 Python
Python修改列表值问题解决方案
2020/03/06 Python
如何在Python对Excel进行读取
2020/06/04 Python
python使用dlib进行人脸检测和关键点的示例
2020/12/05 Python
美国高档百货Nordstrom的折扣店:Nordstrom Rack
2017/11/13 全球购物
DeinDesign德国:设计自己的手机壳
2019/12/14 全球购物
PHP解析URL是哪个函数?怎么用?
2013/05/09 面试题
网络维护中文求职信
2014/01/03 职场文书
人力资源部经理助理岗位职责
2014/03/04 职场文书
实习生评语
2014/04/26 职场文书
农村婚庆主持词
2015/06/29 职场文书
2019年描写人生经典诗句大全
2019/07/08 职场文书
网络安全倡议书(3篇)
2019/09/18 职场文书