php 原生分页类

来源:互联网 发布:linux进入grub界面 编辑:程序博客网 时间:2024/05/18 12:42

闲的无聊用原生php 写一个简单的分页类多多指点:p

<?php class Page{    //一列数量    public $colNum;    //总数    public $count;    //总页数    public $pageCount;    //当前页数    public $nowPage;    //偏移数    public $offestPage;    //完整URL    public $thisUrl;    //显示页码个数    public $pageRoll;    /**    * 初始化分页类的各种参数    *@param $[colNum]  [单页数据数量]    *@param $[count]   [数据总数]    *@param $[roll]    [分页码显示数量]    *@return           []       */    public function __construct($colNum,$count,$roll){        //显示多少个分页数字        $this->pageRoll = $roll;        //一页多少条记录        $this->colNum = $colNum;        //得到当前页面url        $this->thisUrl = $_SERVER["REQUEST_URI"];        //计算总页数        $this->pageCount = ceil($count/$colNum);        //处理当前页        if(empty($_GET['p'])||$_GET['p']<0 || !is_numeric($_GET['p'])){            $this->nowPage = 1;        }else if($_GET['p']>$this->pageCount){            $this->nowPage = $this->pageCount;        }else{            $this->nowPage = (int)$_GET['p'];        }        //计算偏移量        $this->offestPage = ($this->nowPage-1)*$this->colNum;    }    /**    *处理分页逻辑生成分页代码    *@return [str:前端代码分页 html]    */    public function show(){        //没有数据  退出        if( $this->pageCount == 0 ){            return '';        }        $html = '';        //分析当前url         $par = parse_url( $this->thisUrl );        //当前页码  ceil  取整        $nowCool = ceil( $this->nowPage/$this->pageRoll );        //url有参数的情况        if( isset( $par['query'] ) ){            parse_str( $par['query'],$par_array );            unset( $par_array['p'] );            $this->thisUrl = $par['path'].'?'.http_build_query( $par_array ).'&';        }else{            //url 无参数情况            $this->thisUrl = $par['path'].'?';        }        //上一页判断        if( $this->nowPage > 1 ){            $html .= '<a href = '.$this->thisUrl.'p='.($this->nowPage-1).'>'.'上一页'.'</a> &nbsp &nbsp &nbsp' ;        }        for( $i = 1;$i <= $this->pageRoll;$i++ ){            // echo $nowCool.'---'.$this->nowPage;            $page = ($nowCool -1)*$this->pageRoll+$i;            $html .= '<a href = '.$this->thisUrl.'p='.$page.'>'.$page.'</a>&nbsp &nbsp &nbsp' ;        }        //一页判断        if( $this->nowPage < $this->pageCount ){            $html .= '<a href = '.$this->thisUrl.'p='.($this->nowPage+1).'>'.'下一页'.'</a>&nbsp &nbsp &nbsp' ;        }        return $html;    }}//调用部分$db = new mysqli('localhost', 'root', '', 'newecms');$set_utf8 =$db->query('set names utf8');$page = new Page(4,59,3);$sql = 'select name from wen_ushop limit '.$page->offestPage.','.$page->colNum;$res = $db->query($sql);$res = mysqli_fetch_all($res);var_dump($res);echo $page->show();
原创粉丝点击