php简单分页类

php 简单分页类

写这个分页类主要是用来理解php的分页原理

<?php
/**
 * 分页类 
 * 真分页是每次都去数据库查询,增加数据库的负担
 * 假分页是第一次查询,然后读取数据,没有及时性
 */
class pagination {
    private $total; //总记录数
    private $pagesize; //每页显示多少条
    private $limit; //每页显示条数
    private $url;   //自定义url
    private $page;  //当前页码
    private $pagenum; //总页码

    //构造方法
    public function __construct($count,$size) {
        $this->total = $count ? $count : 1;
        $this->pagesize = $size;
        $this->pagenum = ceil($this->total/$this->pagesize);
        $this->page = $this->getPage();
        $this->url = $this->getUrl();
    }

    private function getPage() {
        $page = !empty($_GET['page']) ? $_GET['page'] : 1;
        $page = ($page > 0) ? $page : 1;
        if($page > $this->pagenum) {
            return $this->pagenum;
        } else {
            return $page;
        }
    }

    private function getUrl() {
        $url = $_SERVER['REQUEST_URI'];
        return $url;
    }

    private function replaceUrl($page) {
        //解析url
        $param = parse_url($this->url);
        if(isset($param['query'])) {
            $route = preg_replace('/page=(\d+)/', 'page='.$page, $this->url);
        } else {
            if($page >= 1) {
                $route = $this->url.'?page='.$page;
            } else {
                $route = $this->url;
            }
        }
        return $route;
    }

    //首页|第一页
    private function first() {
        if($this->page != 1) {
            return '<a href="'.$this->replaceUrl(1).'">首页</a>';
        } else {
            return '<span>首页</span>';
        }
    }

    //上一页
    private function prev() {
        if($this->page == 1) {
            return '<span>上一页</span>';
        }
        return '<a href="'.$this->replaceUrl($this->page-1).'">上一页</a>';
    }

    //下一页
    private function next() {
        if($this->page == $this->pagenum) {
            return '<span>下一页</span>';
        }
        return '<a href="'.$this->replaceUrl($this->page+1).'">下一页</a>';
    }

    //尾页
    private function last() {
        if($this->page != $this->pagenum) {
            return '<a href="'.$this->replaceUrl($this->pagenum).'">尾页</a>';
        }
        return '<span>尾页</span>';
    }

    //显示分页信息
    public function show() {
        header("Content-type: text/html; charset=utf-8");
        $str = '<div id="">';
        $str .= $this->first();
        $str .= $this->prev();
        if($this->page > 1) {
            $str .= '<span>...</span>';
        }
        for ($i=1; $i <= $this->pagenum; $i++) { 
            if($i == $this->page) {
                $str .= '<a href="'.$this->replaceUrl($i).'" class="cur">'.$i.'</a>';
            } else {
                $str .= '<a href="'.$this->replaceUrl($i).'">'.$i.'</a>';
            }
        }
        if($this->page < $this->pagenum) {
            $str .= '<span>...</span>';
        }
        $str .= $this->next();
        $str .= $this->last();
        return $str;
    }
}

$page = new pagination(20,3);
echo $page->show();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值