万能的php分页类
程序员文章站
2024-03-11 23:55:31
本文实例为大家分享了php分页类的具体代码,供大家参考,具体内容如下
本文实例为大家分享了php分页类的具体代码,供大家参考,具体内容如下
<?php /*核心:首页、上一页、下一页、尾页的url*/ /*超全局$_server*/ $page = new page(5,60); var_dump($page->allurl()); class page{ // 每页显示的个数 protected $number; // 一共有多少数据 protected $totalcount; // 当前页 protected $page; // 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(); echo $this->url; } /*得到总页数并向上取整*/ protected function gettotalpage(){ return ceil($this->totalcount/$this->number); } /**/ 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; } protected function geturl(){ //得到协议名 $scheme = $_server['request_scheme']; //得到主机名 $host= $_server['server_name']; //得到端口号 $port = $_server['server_port']; //得到路径和请求字符串 $url = $_server['request_uri']; /*中间做处理,要将page=5等这种字符串拼接url 中,所以如果原来的url中有page这个参数,我们首先 需要将原来的page参数给清空*/ $urlarray = parse_url($url); // var_dump($urlarray); $path = $urlarray['path']; if (!empty($urlarray['query'])){ //将query中的值转化为数组 parse_str($urlarray['query'],$array); //如果他有page就将它删掉 unset($array['page']); //将关联数组转化为query $query = http_build_query($array); //不为空的话就与path连结 if ($query != ''){ $path = $path.'?'.$query; } } return 'http://'. $host.':'.$port.$path; } protected function seturl($str){ if (strstr($this->url, '?')){ $url = $this->url.'&'.$str; }else{ $url = $this->url.'?'.$str; } return $url; } /*所有的url*/ public function allurl(){ return [ 'first' => $this->first(), 'next' => $this->next(), 'prev'=> $this->prev(), 'end'=> $this->end(), ]; } /*首页*/ 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(){ return $this->seturl('page='.$this->totalpage); } /*limit 0,5*/ public function limit(){ $offset = ($this->page-1)*$this->number; return $offset.','.$this->number; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。