php分页类

  1 <?php
  2 /*核心:首页、上一页、下一页、尾页的url*/
  3 /*超全局$_SERVER*/
  4 $page = new Page(5,60);
  5 var_dump($page->allUrl());
  6 class Page{
  7 //     每页显示的个数
  8     protected $number;
  9 //     一共有多少数据
 10     protected $totalCount;
 11 //     当前页
 12     protected $page;
 13 //     url
 14     protected $url;
 15     
 16     public function __construct($number,$totalCount){
 17         $this->number= $number;
 18         $this->totalCount = $totalCount;
 19         //得到总页数
 20         $this->totalPage = $this->getTotalPage();
 21         //得到当前页数
 22         $this->page = $this->getPage();
 23         //得到URL
 24         $this->url = $this->getUrl();
 25         echo $this->url;
 26     }
 27     /*得到总页数并向上取整*/
 28     protected  function getTotalPage(){
 29         return     ceil($this->totalCount/$this->number);
 30     }
 31     /**/
 32     protected function getPage(){
 33         if (empty($_GET['page'])){
 34             $page=1;
 35         }elseif ($_GET['page'] > $this->totalPage){
 36             $page = $this->totalPage;
 37         }elseif ($_GET["page"]<1){
 38             $page = 1;
 39         }else{
 40             $page = $_GET['page'];
 41         }
 42         return $page;
 43     }
 44     protected  function getUrl(){
 45         //得到协议名
 46         $scheme = $_SERVER['REQUEST_SCHEME'];
 47         //得到主机名
 48         $host= $_SERVER['SERVER_NAME'];
 49         //得到端口号
 50         $port = $_SERVER['SERVER_PORT'];
 51         //得到路径和请求字符串
 52         $url = $_SERVER['REQUEST_URI'];
 53         /*中间做处理,要将page=5等这种字符串拼接URL
 54         中,所以如果原来的url中有page这个参数,我们首先
 55         需要将原来的page参数给清空*/
 56         $urlArray = parse_url($url);
 57 //         var_dump($urlArray);
 58 
 59         $path = $urlArray['path'];
 60         if (!empty($urlArray['query'])){
 61             //将query中的值转化为数组
 62             parse_str($urlArray['query'],$array);
 63             //如果他有page就将它删掉
 64             unset($array['page']);
 65             //将关联数组转化为query
 66             $query = http_build_query($array);
 67             //不为空的话就与path连结
 68             if ($query != ''){
 69                 $path  = $path.'?'.$query;
 70             }
 71         }
 72         return 'http://'. $host.':'.$port.$path;
 73     }
 74     protected function setUrl($str){
 75         if (strstr($this->url, '?')){
 76             $url = $this->url.'&'.$str;
 77         }else{
 78             $url = $this->url.'?'.$str;
 79         }
 80         return $url;
 81     }
 82     /*所有的url*/
 83     public function allUrl(){
 84         return [
 85             'first' => $this->first(),
 86             'next' => $this->next(),
 87             'prev'=> $this->prev(),
 88             'end'=> $this->end(),
 89         ];
 90     }
 91     /*首页*/
 92     public function first(){
 93         return $this->setUrl('page=1');
 94     }
 95     /*下一页*/
 96     public function next(){
 97         //根据当前page得带下一页的页码
 98         if ($this->page+1 > $this->totalPage) {
 99             $page = $this->totalPage;
100         }else{
101             $page = $this->page+1;
102         }
103         return $this->setUrl('page='.$page);
104     }
105     /*上一页*/
106     public function prev(){
107         //根据当前page得带下一页的页码
108         if ($this->page - 1 < 1) {
109             $page = 1;
110         }else{
111             $page = $this->page-1;
112         }
113         return $this->setUrl('page='.$page);
114     }
115     /*尾页*/
116     public function end(){
117         return $this->setUrl('page='.$this->totalPage);
118     }
119     /*limit 0,5*/
120     public function limit(){
121         $offset = ($this->page-1)*$this->number;
122         return $offset.','.$this->number;
123     }
124     
125 }

 

posted @ 2017-07-05 13:26  东东东蔚  阅读(76)  评论(0编辑  收藏  举报