Home  >  Article  >  Backend Development  >  适用好用的PHP分页类代码与使用

适用好用的PHP分页类代码与使用

WBOY
WBOYOriginal
2016-06-13 12:30:02748browse

实用好用的PHP分页类代码与使用

类的代码如下:

total = $_total ? $_total : 1;
???		$this->pagesize = $_pagesize;
??		$this->pagenum = ceil($this->total / $this->pagesize);
???		$this->page = $this->setPage();
???		$this->limit = "LIMIT ".($this->page-1)*$this->pagesize.",$this->pagesize";
???		$this->url = $this->setUrl();
???		$this->bothnum = 2;
??	}
??
??	//拦截器
??	private function __get($_key) {
???		return $this->$_key;
??	}
??
??	//获取当前页码
??	private function setPage() {
???		if (!empty($_GET['page'])) {
????			if ($_GET['page'] > 0) {
?????				if ($_GET['page'] > $this->pagenum) {
??????					return $this->pagenum;
?????				} else {
??????					return $_GET['page'];
?????				}
????			} else {
?????				return 1;
????			}
???		} else {
????			return 1;
???		}
??	}?
??
??	//获取地址
??	private function setUrl() {
???		$_url = $_SERVER["REQUEST_URI"];
???		$_par = parse_url($_url);
???		if (isset($_par['query'])) {
????			parse_str($_par['query'],$_query);
????			unset($_query['page']);
????			$_url = $_par['path'].'?'.http_build_query($_query);
???		}
???		return $_url;
??	}??	//数字目录
??	private function pageList() {
???		for ($i=$this->bothnum;$i>=1;$i--) {
????		$_page = $this->page-$i;
????		if ($_page < 1) continue;
????			$_pagelist .= ' '.$_page.' ';
???		}
???		$_pagelist .= ' '.$this->page.' ';
???		for ($i=1;$i<=$this->bothnum;$i++) {
???	?		$_page = $this->page+$i;
????			if ($_page > $this->pagenum) break;
????			$_pagelist .= ' '.$_page.' ';
???		}
???		return $_pagelist;
??	}
??
??	//首页  http://ini.iteye.com/
??	private function first() {
???		if ($this->page > $this->bothnum+1) {
????			return ' 1 ...';
???		}
??	}
??
??	//上一页
??	private function prev() {
???		if ($this->page == 1) {
????			return '上一页';
???		}
???		return ' 上一页 ';
??	}
??
??	//下一页
??	private function next() {
???		if ($this->page == $this->pagenum) {
????			return '下一页';
???		}
???		return ' 下一页 ';
??	}
??
??	//尾页
??	private function last() {
???		if ($this->pagenum - $this->page > $this->bothnum) {
????			return ' ...'.$this->pagenum.' ';
???		}
??	}
??
??	//分页信息
??	public function showpage() {
???		$_page .= $this->first();
???		$_page .= $this->pageList();
???		$_page .= $this->last();
???		$_page .= $this->prev();
???		$_page .= $this->next();
???		return $_page;
??	}
?}
?>

??使用说明:

?分页样式如下图所示:?



?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn