php 原生分页类

4 篇文章 0 订阅
3 篇文章 0 订阅

闲的无聊用原生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();

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值