🗣 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地址: 138.227.211.122
搜索次数: 0
提问时间: 2025-10-30 15:44:34
🛒 域名购买:
191.net.cn
dxgm.cn
souru.com.cn
ty.gs
32.cx
app.jl.cn
68.wiki
w-t.cn
lq.gs
vj.gs
❓️ 热门提问:
250挂机网
高防云主机
什么云服务器比较便宜
php工程师如何做缓存
sugon 服务器
网站代码优化
国外的云主机
letvps
免费云主机个人
八度网络vps怎么样
豌豆Ai站群搜索引擎系统
🌐 域名评估:
ppbr.cn
bb.sx.cn
byd.nm.cn
tt.hn.cn
vivo.jl.cn
nn.mo.cn
7773.com.cn
8.sx.cn
oo.hi.cn
au.cq.cn
⛏ 最新挖掘:
凯里酸汤鱼
贵阳烤脑花
都匀毛尖茶
花溪牛肉粉
贵阳烤生蚝
贵阳烤豆腐
贵阳纸包鱼
贵阳特色菜
🖌 热门作画:
🤝 关于我们:
豌豆Ai
域名
建站
站群
留痕
推广
评估
源码
开发Ai
工具
日记
价格
加盟
广告
流量
留言
联系
🗨 加入群聊:
🔗 友情链接:
广州月饼加工
Motor de búsqueda
ai提问
🧰 站长工具:
Ai工具 whois查询 搜索
📢 温馨提示:本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持:本站由豌豆Ai提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。