php高级实战-分页类

<?php

$page = new Page(5, 60);
var_dump($page->allUrl());

class Page {
    //每一页显示多少条数据
    protected $countOfPerPage;
    //一共有多少条数据
    protected $totalCount;
    //当前页
    protected $currPage;
    //总页数
    protected $totalPage;
    //url
    protected $url;

    public function __construct ($countOfPerPage, $totalCount) {
        $this->countOfPerPage = $countOfPerPage;
        $this->totalCount = $totalCount;
        $this->totalPage = $this->getTotalPage();
        $this->page = $this->getPage();

        $this->url = $this->getUrl();

        echo $this->url;
    }

    protected function getUrl () {
        $scheme = $_SERVER['REQUEST_SCHEME'];
        $host = $_SERVER['SERVER_NAME'];
        $port = $_SERVER['SERVER_PORT'];
        $uri = $_SERVER['REQUEST_URI'];

        $uriArray = parse_url($uri);
        $path = $uriArray['path'];

        if (!empty($uriArray['query'])) {
            parse_str($uriArray['query'], $array);
            unset($array['page']);

            $query = http_build_query($array);
            if ('' != $query) {
                $path = $path . '?' . $query;
            }
        }

        return $scheme . '://' . $host . ':' . $port . $path;
    }

    protected function getPage () {
        if (empty($_GET['page'])) {
            $page = 1;
        } else if ($_GET['page'] > $this->totalPage) {
            $page = $this->totalPage;
        } else if ($_GET['page'] < 1) {
            $page = 1;
        } else {
            $page = $_GET['page'];
        }

        return $page;
    }


    protected function getTotalPage () {
        return ceil($this->totalCount / $this->countOfPerPage);
    }

    public function allUrl () {
        return [
            'first' => $this->first(),
            'prev' => $this->prev(),
            'next' => $this->next(),
            'last' => $this->last()
        ];
    }

    public function first () {
        return $this->setUrl('page=1');
    }

    protected function setUrl ($str) {
        if (strstr($this->url, '?')) {
            $url = $this->url . '&' . $str;
        } else {
            $url = $this->url . '?' . $str;
        }

        return $url;
    }

    public function next () {
        if ($this->currPage + 1 > $this->totalPage) {
            $page = $this->totalPage;
        } else {
            $page = $this->currPage + 1;
        }

        return $this->setUrl('page=' . $page);
    }

    public function prev () {
        if ($this->currPage-1 < 1) {
            $page = 1;
        } else {
            $page = $this->currPage - 1;
        }

        return $this->setUrl('page=' . $page);
    }

    public function last () {
        return $this->setUrl('page=' . $this->totalPage);
    }

    public function limit () {
        $offset = ($this->page - 1) * $this->countOfPerPage;

        return $offset . ',' . $this->countOfPerPage;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值