一个简单的php分页类(入门)

发布时间:2019-09-06编辑:脚本学堂
php分页类的简单实现,根据总记录数与每页记录数,然后计算出总页数。供初学的朋友们参考,大家可以自行扩展该代码。

代码如下:

<?php  
    /** 
     * 分页类 
      * www.jb200.com
     * */  
    class Page{  
        private  $total;     //查询总的数据记录  
        private  $page;      //当前第几页  
        //private  $pagesize;  //每页显示的条数  
        private  $pagenum;   //总共多少页  
        private  $num;       //每页显示记录的条数  
        private  $offset;    //从数据库中取记录的开始偏移数  
          
        function __construct($total,$page=1,$num=5){  
            $this->total=$total;  
            $this->page=$page;  
            $this->num=$num;  
            $this->pagenum=$this->getPageNum();  
            $this->offset=$this->getOffset();  
        }  
        private function getPageNum(){  
            return  ceil($this->total/$this->num);  
        }  
        private function getNextPage(){    //下一页  
            if($this->page==$this->pagenum){  
                return false;  
            }else {  
                return $this->page+1;  
            }  
        }  
        private  function getPrevPage(){  
            if($this->page==1){            //上一页  
                return false;  
            }else {  
                return $this->page-1;  
            }  
        }  
        private  function getOffset(){  
            return  ($this->page-1)*$this->num;  
        }  
        private function getStartNum(){  
            if ($this->total==0) {  
                return 0;  
            }else {  
                return $this->offset+1;  
            }  
        }  
        private  function getEndNum(){  
            return min($this->offset+$this->num,$this->total);  
        }  
        public function getPageInfo(){  
            $pageInfo=array(  
             "row_total"=>$this->total,  
             "row_num"  =>$this->num,  
             "page_num" =>$this->getPageNum(),  
             "current_page"=>$this->page,  
             "row_offset"=>$this->getOffset(),  
             "next_page"=>$this->getNextPage(),  
             "prev_page"=>$this->getPrevPage(),  
             "page_start"=>$this->getStartNum(),  
             "page_end" =>$this->getEndNum()  
            );  
            return  $pageInfo;  
        }  
    }  
?>