这篇文章主要为大家详细介绍了万能的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;
}
}
以上就是php万能分页类分享的详细内容。