栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP中面向对象的分页类

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP中面向对象的分页类

 

$page = new Page(53,10);

$p = $page->rendor();

echo '

';

var_dump($p);

echo'

';

class Page

{

    protected $url;            //URL

    protected $pageCount;  //总页数

    protected $total;      //总条数

    protected $num;            //每页显示数

    protected $page;       //当前页

 

    //初始化成员属性

    public function __construct($total,$num = 5)

    {

        //总条数

        $this->total = ($total > 0 )? (int) $total : 1;

        //每页显示条数

        $this->num = $num;

        //总页数

        $this->pageCount = $this->getPageCount();

        //当前页

        $this->page = $this->getCurrentPage();

 

        //URL

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

    }

    //一次性返回所有的分页信息

    public function rendor()

    {

        return[

            'first' => $this->first(),

            'next' => $this->next(),

            'prev' => $this->prev(),

            'end' => $this->end(),

 

 

        ];

    }

    //limit方法,在未来分页数据查询的时候,直接返回对应的limit0,5 这样的字符串

    public function limit()

    {

        $offset = ($this->page - 1)  * $this->num;

        $str = $offset.','.$this->num;

        return $str;

    }

 

    //首页,设置page = 1

    protected function first()

    {

        return $this->setQueryString('page=1');

 

 

    }

 

    //上一页

    protected function prev()

    {

        $page = ($this->page <= 1) ? 1 : ($this->page - 1);

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

 

    }

    //下一页

    protected function next()

    {

        $page = ($this->page >= $this->pageCount) ? $this->pageCount : ($this->page + 1);

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

 

    }

    //首页

    protected function end()

    {

        return $this->setQueryString('page='.$this->pageCount);

    }

 

    //一种情况有? 一种情况没有?

    protected function setQueryString($page)

    {

        //查找url中是否有问号

        if (stripos($this->url, '?')) {

            return $this->url.'&'.$page;

        } else {

            //没有问号就拼接

            return $this->url.'?'.$page;

        }

 

    }

 

    //处理URL

    protected function getUrl()

    {

        //获取用户的uri

        $path = $_SERVER['REQUEST_URI'];

 

        //解析uri

        $par = parse_url($path);

 

        //判断用户是否设置过query

        if (isset($par['query'])) {

            parse_str($par['query'],$query);

            //检查query 里面时候有page,如果有的话就干掉page

            unset($query['page']);

            $path = $par['path'] . '?'.http_build_query($query);

        }

 

        $path = rtrim($path,'?');

 

 

        //协议:主机:端口:文件和请求

        //判断是否定义过端口,并且端口是否为443,如果为443则是https协议,否则就是http协议

        $protocal = (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) ? 'https//' : 'http://';

        if (80 == $_SERVER['SERVER_PORT'] || 443 == $_SERVER['SERVER_PORT']) {

            $url = $protocal.$_SERVER['SERVER_NAME'].$path;

        }else{

            $url = $protocal.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$path;

        }

        //全新的url

        return $url;

 

    }

    //处理当前页Page

    protected function getCurrentPage()

    {

        //如果有page,就返回转换为int的page

        if (isset($_GET['page'])) {

            //得到页码

            $page = (int) $_GET['page'];

 

            //当前页码不能给大于总页码

            if ($page > $this->pageCount) {

                $page = $this->pageCount;

            }

            if ($page < 1) {

                $page = 1;

            }

        }else {

            $page = 1;

            }

        return $page;

        }

 

 

    //处理总页数

    protected function getPageCount()

    {

        //进一法取整

        return ceil($this->total / $this->num);

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/228946.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号