当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP学习之分页类

PHP学习之分页类

2019年05月30日  | 移动技术网IT编程  | 我要评论
<?php
$page = new page(2, 40);
var_dump($page->allurl());
class page
{
    //每页显示多少条数据
    protected $number;
    //一共有多少条数据
    protected $totalcount;
    //当前页
    protected $page;
    //总页数
    protected $totalpage;
    //url
    protected $url;

    public function __construct($number, $totalcount)
    {
        $this->number = $number;
        $this->totalcount = $totalcount;
        //得到总页数
        $this->totalpage = $this->gettotalpage();
        //得到当前页数
        $this->page = $this->getpage();
        //得到url
        $this->url = $this->geturl();
    }

    /**
     * 获取总页数
     *
     * @return void
     */
    protected function gettotalpage()
    {
        return ceil($this->totalcount / $this->number);
    }

    /**
     * 获取当前页码
     *
     * @return void
     */
    protected function getpage()
    {
        if (empty($_get['page'])) {
            $page = 1;
        } elseif ($_get['page'] > $this->totalpage) {
            $page = $this->totalpage;
        } elseif ($_get['page'] < 1) {
            $page = 1;
        } else {
            $page = $_get['page'];
        }
        return $page;
    }

    /**
     * 获取去掉page之后的url
     *
     * @return void
     */
    protected function geturl()
    {
        //得到协议名
        $scheme = $_server['request_scheme'];
        //得到主机名
        $host = $_server['server_name'];
        //得到端口号
        $port = $_server['server_port'];
        //得到路径和请求字符串
        $uri = $_server['request_uri'];
        //中间做处理,要将page=5等这种字符串拼接url中,所以如果原来url中有page这个参数,我们首先需要先将原来的page参数给清空
        $uriarray = parse_url($uri);
        $path = $uriarray['path'];
        if (!empty($uriarray['query'])) {
            //首先将请求字符串变为关联数组
            parse_str($uriarray['query'], $array);
            //清除掉关联数组中的page键值对
            unset($array['page']);
            //将剩下的参数拼接为请求字符串
            $query = http_build_query($array);
            //再将请求字符串拼接到路径的后面
            if ($query != '') {
                $path = $path . '?' . $query;
            }
        }
        return $scheme . '://' . $host . ':' . $port . $path;
    }

    /**
     * 设置url
     *
     * @param [type] $str
     * @return void
     */
    protected function seturl($str)
    {
        if (strstr($this->url, '?')) {
            $url = $this->url . '&' . $str;
        } else {
            $url = $this->url . '?' . $str;
        }
        return $url;
    }

    /**
     * 返回所有url
     *
     * @return void
     */
    public function allurl()
    {
        return [
            'first' => $this->first(),
            'prev' => $this->prev(),
            'next' => $this->next(),
            'end' => $this->end()
        ];
    }

    /**
     * 首页
     *
     * @return void
     */
    public function first()
    {
        return $this->seturl('page=1');
    }

    /**
     * 下一页
     *
     * @return void
     */
    public function next()
    {
        //根据当前page得到下一页的页码
        if ($this->page + 1 > $this->totalpage) {
            $page = $this->totalpage;
        } else {
            $page = $this->page + 1;
        }
        return $this->seturl('page=' . $page);
    }

    /**
     * 上一页
     *
     * @return void
     */
    public function prev()
    {
        if ($this->page - 1 < 1) {
            $page = 1;
        } else {
            $page = $this->page - 1;
        }
        return $this->seturl('page=' . $page);
    }

    /**
     * 尾页
     *
     * @return void
     */
    public function end()
    {
        return $this->seturl('page=' . $this->totalpage);
    }

    /**
     * 偏移量,方便数据库查找
     *
     * @return void
     */
    public function limit()
    {
        $offset = ($this->page - 1) * $this->number;
        return $offset . ',' . $this->number;
    }
}

运行结果:

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网