🗣 Wd提问: php分页工厂
🤖 Ai回答: 以下是一个 PHP 分页工厂的实现方案,支持多种分页场景和自定义配置:
php
class Paginator {
private $total;
private $perPage;
private $currentPage;
private $urlPattern;
private $options;
public function __construct(int $total, int $perPage = 15, int $currentPage = 1, array $options = []) {
$this-total = max(0, $total);
$this-perPage = max(1, $perPage);
$this-currentPage = max(1, min($currentPage, $this-getLastPage()));
$this-urlPattern = $options['url'] ?? '/page/{page}';
$this-options = $options + [
'prev_label' = 'Previous',
'next_label' = 'Next',
'ellipsis' = '...',
'window_size' = 2,
];
}
public function getPerPage(): int {
return $this-perPage;
}
public function getCurrentPage(): int {
return $this-currentPage;
}
public function getLastPage(): int {
return ceil($this-total / $this-perPage) ?: 1;
}
public function hasPages(): bool {
return $this-total $this-perPage;
}
public function getOffset(): int {
return ($this-currentPage 1) * $this-perPage;
}
public function getLinks(): array {
$pages = [];
$lastPage = $this-getLastPage();
$windowSize = $this-options['window_size'];
$start = max(1, $this-currentPage $windowSize);
$end = min($lastPage, $this-currentPage + $windowSize);
if ($start 1) {
$pages[] = ['label' = 1, 'url' = $this-buildUrl(1)];
if ($start 2) {
$pages[] = ['label' = $this-options['ellipsis'], 'disabled' = true];
}
}
for ($i = $start; $i <= $end; $i++) {
$pages[] = [
'label' = $i,
'url' = $this-buildUrl($i),
'active' = $i === $this-currentPage
];
}
if ($end < $lastPage) {
if ($end < $lastPage 1) {
$pages[] = ['label' = $this-options['ellipsis'], 'disabled' = true];
}
$pages[] = ['label' = $lastPage, 'url' = $this-buildUrl($lastPage)];
}
return [
'prev' = $this-buildLink('prev'),
'pages' = $pages,
'next' = $this-buildLink('next')
];
}
private function buildUrl(int $page): string {
return str_replace('{page}', $page, $this-urlPattern);
}
private function buildLink(string $type): array {
$current = $this-currentPage;
switch ($type) {
case 'prev':
$page = max(1, $current 1);
break;
case 'next':
$page = min($this-getLastPage(), $current + 1);
break;
default:
throw new InvalidArgumentException(Invalid link type: $type);
}
return [
'label' = $this-options[{$type}_label],
'url' = $this-buildUrl($page),
'disabled' = $page === $current
];
}
}
使用示例:
php
// 假设从数据库获取了总记录数
$totalRecords = 1234;
$paginator = new Paginator($totalRecords, 20, $_GET['page'] ?? 1, [
'url' = '/products?page={page}',
'prev_label' = '← Previous',
'next_label' = 'Next →',
'window_size' = 3
]);
// 获取分页数据
$data = DB::query(SELECT * FROM products LIMIT {$paginator-getOffset()}, {$paginator-getPerPage()});
// 渲染分页导航
if ($paginator-hasPages()) {
$links = $paginator-getLinks();
echo '<nav<ul class=pagination';
echo <li class='page-item {$links['prev']['disabled'] ? 'disabled' : ''}'
<a href='{$links['prev']['url']}' class='page-link'{$links['prev']['label']}</a
</li;
foreach ($links['pages'] as $page) {
echo <li class='page-item {$page['active'] ? 'active' : ''}'
<a href='{$page['url']}' class='page-link'{$page['label']}</a
</li;
}
echo <li class='page-item {$links['next']['disabled'] ? 'disabled' : ''}'
<a href='{$links['next']['url']}' class='page-link'{$links['next']['label']}</a
</li;
echo '</ul</nav';
}
关键特性:
1、动态窗口:根据当前页码自动显示相邻页码(默认显示前后各2页)
2、智能省略号:当页码过多时自动添加省略号
3、URL构建器:支持自定义分页URL模式
4、状态标识:提供激活/禁用状态标记
5、扩展性:可通过$options参数自定义标签文本、窗口大小等
6、错误处理:自动修正无效页码请求
7、性能优化:仅在需要时才生成完整链接数组
该实现遵循单一职责原则,将分页逻辑与视图渲染分离,适合集成到MVC框架中。
📣 商家广告:





0
IP地址: 188.189.127.69
搜索次数: 0
提问时间: 2025-10-30 15:44:34
🛒 域名购买:
qw.gs
8yw.net
7mt.net
82.chat
guzun.cn
61.gs
uj.gs
te.biz
85.com.hk
touwang.com.cn
❓️ 热门提问:
搭建帕鲁服务器
电脑一主机
电脑服务器怎么打开
idc机房托管收费标准
域名注册服务器托管
对云计算的认识
电脑主机的
16u服务器机柜
电脑挂机的游戏
windowsftp服务器
豌豆Ai站群搜索引擎系统
🌐 域名评估:
sun.tw.cn
hkza.cn
urtj.cn
juhc.cn
a.cx
pp.hl.cn
wddd.com.cn
826.net.cn
pp.sn.cn
pp.sd.cn
⛏ 最新挖掘:
凯里酸汤鱼
贵阳烤脑花
都匀毛尖茶
花溪牛肉粉
贵阳烤生蚝
贵阳烤豆腐
贵阳纸包鱼
贵阳特色菜
🖌 热门作画:
🤝 关于我们:
豌豆Ai
域名
建站
站群
留痕
推广
评估
源码
开发Ai
工具
日记
价格
加盟
广告
流量
留言
联系
🗨 加入群聊:
🔗 友情链接:
Search engine
谷歌排名优化
ai提问
🧰 站长工具:
Ai工具 whois查询 搜索
📢 温馨提示:本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持:本站由豌豆Ai提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。