亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

PHP中面向對象的分頁類

標簽:
PHP

<?php

 

$page = new Page(53,10);

$p = $page->rendor();

echo '<pre>';

var_dump($p);

echo'</pre>';

/**

* 分页类

*/

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);

    }

}

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消