简单好用的PHP分页类
作者:我才是撸大师 时间:2023-11-22 09:32:39
本文为大家分享了两段PHP分页类,很实用,供大家参考,具体内容如下
<?php
class Page {
private $total; //总记录
private $pagesize; //每页显示多少条
private $limit; //limit
private $page; //当前页码
private $pagenum; //总页码
private $url; //地址
private $bothnum; //两边保持数字分页的量
//构造方法初始化
public function __construct($_total, $_pagesize) {
$this->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 .= ' <a href="'.$this->url.'&page='.$_page.'">'.$_page.'</a> ';
}
$_pagelist .= ' <span class="me">'.$this->page.'</span> ';
for ($i=1;$i<=$this->bothnum;$i++) {
$_page = $this->page+$i;
if ($_page > $this->pagenum) break;
$_pagelist .= ' <a href="'.$this->url.'&page='.$_page.'">'.$_page.'</a> ';
}
return $_pagelist;
}
//首页
private function first() {
if ($this->page > $this->bothnum+1) {
return ' <a href="'.$this->url.'">1</a> ...';
}
}
//上一页
private function prev() {
if ($this->page == 1) {
return '<span class="disabled">上一页</span>';
}
return ' <a href="'.$this->url.'&page='.($this->page-1).'">上一页</a> ';
}
//下一页
private function next() {
if ($this->page == $this->pagenum) {
return '<span class="disabled">下一页</span>';
}
return ' <a href="'.$this->url.'&page='.($this->page+1).'">下一页</a> ';
}
//尾页
private function last() {
if ($this->pagenum - $this->page > $this->bothnum) {
return ' ...<a href="'.$this->url.'&page='.$this->pagenum.'">'.$this->pagenum.'</a> ';
}
}
//分页信息
public function showpage() {
$_page .= $this->first();
$_page .= $this->pageList();
$_page .= $this->last();
$_page .= $this->prev();
$_page .= $this->next();
return $_page;
}
}
?>
分页样式如下图所示:
使用说明:
<?php
$_page = new Page($_total,$_pagesize); //其中 $_total 是数据集的总条数,$_pagesize 是每页显示的数量.
?>
第二个分页类:
<?php
/**
* page.class.php 分页类
* 日期:2015-04-05
* http://aowana.sinaapp.com
*/
class page
{
public static $url;
public static $page;
public static $total;
public static $size;
public static $number;
public static $max;
public static $action = array('previous', 'number', 'next');
public static function show($url, $page, $total, $size=10, $number=11)
{
self::$url = $url;
self::$page = $page;
self::$total = $total;
self::$size = $size;
self::$number = $number;
self::$max = ceil($total / $size);
$str = '';
foreach (self::$action as $name)
$str .= self::$name();
return $str;
}
public static function first()
{
if (self::$page > 2)
{
$url = self::url(1);
return "<li id=\"pbfirst\"><a href=\"{$url}\" title=\"首页\">首页</a></li>";
}
}
public static function previous()
{
if (self::$page > 1)
{
$url = self::url(self::$page - 1);
return "<li id=\"pbprevious\"><a href=\"{$url}\" title=\"上一页\">上一页</a></li>";
}
}
public static function number()
{
$str = '';
$f = self::$number % 2 ? (self::$number - 1) / 2 : self::$number / 2;
$s = self::$page - $f;
$e = self::$page + $f;
if (self::$page < ($f + 1))
{
$s = 1;
$e = self::$number;
$e = $e > self::$max ? self::$max : $e;
}
if (self::$page > (self::$max - $f))
{
$s = self::$max - self::$number;
$s = $s < 1 ? 1 : $s;
$e = self::$max;
}
for ($i=$s; $i<=$e; $i++)
{
$url = self::url($i);
if ($i == self::$page)
$str .= "<li><strong>{$i}</strong></li>";
else
$str .= "<li><a href=\"{$url}\" title=\"第{$i}页\">{$i}</a></li>";
}
return $str;
}
public static function next()
{
if (self::$page < self::$max)
{
$url = self::url(self::$page + 1);
return "<li id=\"pbnext\"><a href=\"{$url}\" title=\"下一页\">下一页</a></li>";
}
}
public static function end()
{
if ((self::$max - self::$page) > 1)
{
$url = self::url(self::$max);
return "<li id=\"pbend\"><a href=\"{$url}\" title=\"末页\">末页</a></li>";
}
}
public static function url($page)
{
return str_replace('{page}', $page, self::$url);
}
}
标签:PHP,分页类
0
投稿
猜你喜欢
pyqt5 QListWidget的用法解析
2023-09-24 21:30:31
python 模拟登陆github的示例
2022-01-05 17:52:41
Python开发网站目录扫描器的实现
2022-07-09 11:51:02
为网站代码块pre标签增加一个复制代码按钮代码
2024-04-10 10:49:27
PyCharm-错误-找不到指定文件python.exe的解决方法
2022-04-20 00:48:10
vue3中使用Apache ECharts的详细方法
2024-04-26 17:41:01
asp网上购物车实例代码
2007-10-03 13:43:00
python 将html转换为pdf的几种方法
2023-03-30 11:50:11
Flask-Sqlalchemy的基本使用详解
2023-06-16 19:40:02
python图像处理基本操作总结(PIL库、Matplotlib及Numpy)
2022-01-26 00:28:50
python实现图片批量压缩程序
2021-11-27 03:50:11
ie9后浏览器fckeditor无法上传图片、弹出浮层内容不显示的解决方法
2023-05-08 03:34:30
Python入门学习之字符串与比较运算符
2023-10-14 22:09:40
如何正确显示模式对话框showModalDialog中的中文?
2010-06-28 18:24:00
小谈用户身体语言的阅读经验
2009-10-19 20:52:00
解决matplotlib.pyplot在Jupyter notebook中不显示图像问题
2022-03-03 14:55:10
Python实现制度转换(货币,温度,长度)
2022-01-02 00:42:25
树莓派4B安装Tensorflow的方法步骤
2023-09-05 17:28:06
windows上彻底删除jupyter notebook的实现
2022-06-29 13:06:32
Go语言的IO库那么多纠结该如何选择
2023-10-08 07:16:46