Home >php教程 >php手册 >php完美分页类程序

php完美分页类程序

WBOY
WBOYOriginal
2016-06-13 10:00:581694browse

本文章介绍这个php分页类支持下一页,下页,首页等所有分页要有的尽有哦,有需要的同学可以参考一下下哈。

 代码如下 复制代码

/************************************************************************************
* 完美分页类,可以自定义分页(比高胖老师的完美哈,O(∩_∩)O哈哈~)                               *
************************************************************************************
* 版权所有 (C) 2011-2012 北海情书-一个程序员博客,并保留所有权利。                 *
                                 *
* **********************************************************************************
* $Author: 北海情书 (hackmyth@163.com) $                                           *
* $Date: 2011-07-18 10:00:00 $                                                     *
* **********************************************************************************/

    class Page{
   
        private $total;         //数据总条数
        private $listRows;      //每页显示条数
        private $limit;         //限制条数
        private $pageNum;       //总页数
        private $page;          //当前页
        private $config=array('head'=>"条记录", "prev"=>"上一页", "next"=>"下一页", "first"=>"首页", "last"=>"末页");
     
        /**
         * 构造方法,可以设置分页类的属性
         * @param        int        $total                计算分页的总记录数
         * @param        int        $listRows        可选的,默认每页需要显示的记录数
         *
         */
        public function __construct($total,$listRows=10){
        
                $this->total=$total;
                $this->listRows=$listRows;
                $this->pageNum=ceil($this->total/$this->listRows);   //获取总页数
                if($_GET['page']>$this->pageNum){      //防止过界      
                       $_GET['page']=$this->pageNum;
                }
                $this->page=!empty($_GET['page'])?$_GET['page']:"1"; //当前页
                $this->limit=$this->setPage();         //分页公式
        }

        public function __get($args){
              
                if($args=='limit'){            //过滤非法请求
                        return $this->limit;
                }
                return null;
        }

        //计算分页公式
        private function setPage(){

           return "limit ".($this->page-1)*$this->listRows.",".$this->listRows;
       
        }

        //上一页
        private function prev(){
                return "".$this->config['prev']."";
        }
      
        //下一页
        private function next(){
                return "".$this->config['next']."";
        }

        //首页
        private function first(){
                return "".$this->config['first']."";
        }

        //尾页
        private function last(){
                return "".$this->config['last']."";
        }

    

        //分页列表
       
        private function pagelist(){

                $lists.='';
                for($i=1;$ipageNum;$i++){

                    $lists.="$i  ";
                }
                return $lists;
        }


        public function fpage($display=array(1,2,3,4,5)){
       
             $html[1]= "共有".$this->total.$this->config['head'];       
             $html[2]= $this->prev();  //上一页
             $html[3]= $this->next();  //下一页
             $html[4]= $this->first(); //首页
             $html[5]= $this->last();  //尾页
             $html[6]= $this->pagelist();  //列表
             $fpage='';
             foreach($display as $index){
                       
                  $fpage.=$html[$index];
             }
             return $fpage;
        }
   
   
    }

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn