一款简单实用的原生PHP分页类
<?php
/**
* 简单分页类
*/
class PageClass {
private $total; //总记录数
private $pagesize; //每页显示多少条
private $limit; //每页显示条数
private $url; //自定义url
private $page; //当前页码
private $pagenum; //总页码
//构造方法
public function __construct($count, $size) {
$this->total = $count ? $count : 1;
$this->pagesize = $size;
$this->pagenum = ceil($this->total / $this->pagesize);
$this->page = $this->getPage();
$this->url = $this->getUrl();
}
private function getPage() {
$page = !empty($_GET['page']) ? $_GET['page'] : 1;
$page = ($page > 0) ? $page : 1;
if ($page > $this->pagenum) {
return $this->pagenum;
} else {
return $page;
}
}
private function getUrl() {
$url = $_SERVER['REQUEST_URI'];
return $url;
}
private function replaceUrl($page) {
//解析url
$param = parse_url($this->url);
if (isset($param['query'])) {
$route = preg_replace('/page=(\d+)/', 'page=' . $page, $this->url);
} else {
if ($page >= 1) {
$route = $this->url . '?page=' . $page;
} else {
$route = $this->url;
}
}
return $route;
}
//首页|第一页
private function first() {
if ($this->page != 1) {
return '<a href="' . $this->replaceUrl(1) . '">首页</a>';
} else {
return '<span>首页</span>';
}
}
//上一页
private function prev() {
if ($this->page == 1) {
return '<span>上一页</span>';
}
return '<a href="' . $this->replaceUrl($this->page - 1) . '">上一页</a>';
}
//下一页
private function next() {
if ($this->page == $this->pagenum) {
return '<span>下一页</span>';
}
return '<a href="' . $this->replaceUrl($this->page + 1) . '">下一页</a>';
}
//尾页
private function last() {
if ($this->page != $this->pagenum) {
return '<a href="' . $this->replaceUrl($this->pagenum) . '">尾页</a>';
}
return '<span>尾页</span>';
}
//显示分页信息
public function show() {
header("Content-type: text/html; charset=utf-8");
$str = '<div id="">';
$str .= $this->first();
$str .= $this->prev();
if ($this->page > 1) {
$str .= '<span>...</span>';
}
for ($i = 1; $i <= $this->pagenum; $i++) {
if ($i == $this->page) {
$str .= '<a href="' . $this->replaceUrl($i) . '" class="cur">' . $i . '</a>';
} else {
$str .= '<a href="' . $this->replaceUrl($i) . '">' . $i . '</a>';
}
}
if ($this->page < $this->pagenum) {
$str .= '<span>...</span>';
}
$str .= $this->next();
$str .= $this->last();
return $str;
}
}
$page = new PageClass(15, 3);
echo $page->show();
出处:www.l1mn.com
原文标题:php简单的分页类
原文地址:https://www.l1mn.com/p/48beq1.html
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
Copyright © L1MN.COM 联系方式:l1mnfw@163.com