Blogger Information
Blog 25
fans 0
comment 0
visits 29653
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP分页类
宿州市筋斗云信息科技-Vip
Original
773 people have browsed it

PHP分页类编写学习笔记,先来几张课前项目分析的图片……

IMG1.png

IMG2.png

<?php
/**
 * 数据翻页类
 * User: dodo
 * Date: 2017/12/27 0027
 * Time: 上午 3:36
 */
require 'common.php';
class Page{
    //每页显示多少数据
    protected $number;
    //数据总数
    protected $totalCount;
    //一共可以分多少页
    protected $totalPage;
    //当前页面是第几页
    protected $page;
    //url
    protected $url;

    public funnction __construct($number,$totalCount)
    {
        $this->number = $number;
        $this->totalCount = $totalCount;
        //得到的分页总数
        $this->totalPage = $this->getTotalPage();
        //得到当前页码
        $thiss->page = $this->getPage();
        //得到URL
        $this -> url = $this->getUrl();
    }

    //获取分页总数
    protected function getTotalPage()
    {
        //数据总数除以每页显示数向上取整得到  总共分页的数
        return ceil($this->totalCount / $this->number);
    }
    //获取当前页码
    protected function getPage()
    {
            //判断get方式传递的页码是否为空,
            //1.如果为空 强制 页码为1
        if(empty($_GET['page'])){
            $page = 1;
            //2.如果大于最大页数  强制为当前能够显示的最大页码
        }else if($_GET['page'] > $this->totalPage){
            $page = $this->totalPage;
            //3.如果页码数小于1  强制为1
        }else if($_GET['page'] < 1){
            $page = 1;
            //4.传递参数正确。
        }else{
            $page = $_GET['page']
        }
        return $page;
    }
    //获取当前页码
    protected function getUrl()
    {
        //协议名
        $scheme = $_SERVER['REQUEST_SCHEME'];
        //主机名
        $host = $_SERVER['SERVER_NAME'];
        //得到端口号
        $port = $_SERVER['SERVER_PORT'];
        //得到路径地址 和请求字符串
        $uri = $_SERVER['REQUEST_URI'];

        //处理URL 重写URL处理掉PAGE=12323
        $uriArray = parse_url($uri);
        $path = $uriArray['path'];
        if(!empty($uriArray['query'])){
            //将get请求的字符串改成关联数字
            parse_str($uriArray['query'],$array);
            //清除掉关联数组中的page键值对
            unset($array['page']);
            //将剩下的字符串拼接为请求字符串
            $query = http_build_query($array);
            //拼接字符串到路径后面
            if($array !=''){
                $path = $path.'?'.$query;
            }
            return $scheme . '://'.$host.':'.$port.$path;
        }
    }
    protected function setUrl($str)
    {
        if(strstr($this->url,'?')){
            $url = $this-> url.'&'.$str;
        }else{
            $url = $this-> url.'?'.$str;
        }
        return $url;
    }
    //得到第一页
    public function first()
    {
        return $this-> setUrl('page=1');
    }

    //得到下一页
    public function next()
    {
        //根据当前page得到下一页的页码
        if($this->page+1 > $this->totalPage){
            $page = $this->totalPage;
        }else{
            $page = $this->page+1
        }
        return $this-> setUrl('page='.$page);
    }
    //得到上一页
    public function prev()
    {
        //根据当前page得到下一页的页码
        if($this->page - 1 <1){
            $page = 1;
        }else{
            $page = $this->page - 1
            }
        return $this-> setUrl('page='.$page);
    }
    //得到最后一页
    public function end()
    {
        //根据当前page得到下一页的页码
        return $this-> setUrl('page='.$this->totalPage);
    }
    //得到全部URL
    public function allUrl()
    {
        return [
            'first'=>$this->first();
            'prev'=>$this->prev();
            'next'=>$this->next();
            'end'=>$this->end();
        ];
    }
    //limit
    public function limit()
    {
        $offset = ($this->page-1) * $this->number;
        return $offset.','.$this->number;
    }
}


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post