🗣 Wd提问: php分页get
🤖 Ai回答: 我来为您详细介绍PHP分页中GET参数的使用方法。
基础分页实现
1、简单分页示例
php
<?php
// 获取当前页码,如果没有传参则默认为第一页
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$page = max(1, $page); // 确保页码至少为1
// 每页显示的记录数
$per_page = 10;
// 计算偏移量
$offset = ($page 1) * $per_page;
// 示例:查询数据库
$pdo = new PDO('mysql:host=localhost;dbname=test', $username, $password);
// 获取总记录数
$stmt = $pdo-query(SELECT COUNT(*) FROM users);
$total_records = $stmt-fetchColumn();
// 计算总页数
$total_pages = ceil($total_records / $per_page);
// 获取当前页的数据
$stmt = $pdo-prepare(SELECT * FROM users LIMIT :limit OFFSET :offset);
$stmt-bindValue(':limit', $per_page, PDO::PARAM_INT);
$stmt-bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt-execute();
$users = $stmt-fetchAll(PDO::FETCH_ASSOC);
?
<! 显示数据
<table
<thead
<tr
<thID</th
<th用户名</th
<th邮箱</th
</tr
</thead
<tbody
<?php foreach ($users as $user): ?
<tr
<td<?php echo htmlspecialchars($user['id']); ?</td
<td<?php echo htmlspecialchars($user['username']); ?</td
<td<?php echo htmlspecialchars($user['email']); ?</td
</tr
<?php endforeach; ?
</tbody
</table
<! 分页导航
<div class=pagination
<?php if ($page 1): ?
<a href=?page=<?php echo $page 1; ?上一页</a
<?php endif; ?
<?php for ($i = 1; $i <= $total_pages; $i++): ?
<?php if ($i == $page): ?
<span class=current<?php echo $i; ?</span
<?php else: ?
<a href=?page=<?php echo $i; ?<?php echo $i; ?</a
<?php endif; ?
<?php endfor; ?
<?php if ($page < $total_pages): ?
<a href=?page=<?php echo $page + 1; ?下一页</a
<?php endif; ?
</div
2、带搜索条件的分页
php
<?php
// 获取GET参数
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$category = isset($_GET['category']) ? (int)$_GET['category'] : 0;
// 参数验证和清理
$page = max(1, $page);
$search = htmlspecialchars($search, ENT_QUOTES, 'UTF-8');
$per_page = 10;
$offset = ($page 1) * $per_page;
// 构建查询条件
$where_clause = WHERE 1=1;
$params = [];
if (!empty($search)) {
$where_clause .= AND (title LIKE :search OR content LIKE :search);
$params[':search'] = %{$search}%;
}
if ($category 0) {
$where_clause .= AND category_id = :category;
$params[':category'] = $category;
}
// 获取总记录数
$stmt = $pdo-prepare(SELECT COUNT(*) FROM articles {$where_clause});
foreach ($params as $key = $value) {
$stmt-bindValue($key, $value);
}
$stmt-execute();
$total_records = $stmt-fetchColumn();
// 获取当前页数据
$stmt = $pdo-prepare(SELECT * FROM articles {$where_clause} ORDER BY created_at DESC LIMIT :limit OFFSET :offset);
foreach ($params as $key = $value) {
$stmt-bindValue($key, $value);
}
$stmt-bindValue(':limit', $per_page, PDO::PARAM_INT);
$stmt-bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt-execute();
$articles = $stmt-fetchAll(PDO::FETCH_ASSOC);
$total_pages = ceil($total_records / $per_page);
?
<! 搜索表单
<form method=GET action=
<input type=text name=search value=<?php echo $search; ? placeholder=搜索文章...
<select name=category
<option value=0全部分类</option
<option value=1 <?php echo $category == 1 ? 'selected' : ''; ?新闻</option
<option value=2 <?php echo $category == 2 ? 'selected' : ''; ?技术</option
</select
<input type=hidden name=page value=1 <! 重置到第一页
<button type=submit搜索</button
</form
<! 显示结果
<?php foreach ($articles as $article): ?
<div class=article
<h3<?php echo htmlspecialchars($article['title']); ?</h3
<p<?php echo htmlspecialchars(substr($article['content'], 0, 100)) 、'...'; ?</p
</div
<?php endforeach; ?
<! 分页导航(保留搜索参数)
<div class=pagination
<?php
// 构建基础URL参数
$base_url = '?';
$url_params = [];
if (!empty($search)) {
$url_params[] = 'search=' 、urlencode($search);
}
if ($category 0) {
$url_params[] = 'category=' 、$category;
}
if (!empty($url_params)) {
$base_url .= implode('&', $url_params) 、'&';
}
?
<?php if ($page 1): ?
<a href=<?php echo $base_url; ?page=<?php echo $page 1; ?上一页</a
<?php endif; ?
<?php for ($i = 1; $i <= $total_pages; $i++): ?
<?php if ($i == $page): ?
<span class=current<?php echo $i; ?</span
<?php else: ?
<a href=<?php echo $base_url; ?page=<?php echo $i; ?<?php echo $i; ?</a
<?php endif; ?
<?php endfor; ?
<?php if ($page < $total_pages): ?
<a href=<?php echo $base_url; ?page=<?php echo $page + 1; ?下一页</a
<?php endif; ?
</div
3、分页类封装
php
<?php
class Pagination {
private $total_records;
private $per_page;
private $current_page;
private $total_pages;
private $url_params;
public function __construct($total_records, $per_page = 10, $current_page = 1) {
$this-total_records = (int)$total_records;
$this-per_page = (int)$per_page;
$this-current_page = max(1, (int)$current_page);
$this-total_pages = ceil($this-total_records / $this-per_page);
$this-url_params = $this-getUrlParams();
}
private function getUrlParams() {
$params = [];
foreach ($_GET as $key = $value) {
if ($key !== 'page') { // 排除page参数
$params[$key] = $value;
}
}
return $params;
}
public function getOffset() {
return ($this-current_page 1) * $this-per_page;
}
public function hasPrev() {
return $this-current_page 1;
}
public function hasNext() {
return $this-current_page < $this-total_pages;
}
public function getPrevPageUrl() {
if (!$this-hasPrev()) return null;
$params = $this-url_params;
$params['page'] = $this-current_page 1;
return '?' 、http_build_query($params);
}
public function getNextPageUrl() {
if (!$this-hasNext()) return null;
$params = $this-url_params;
$params['page'] = $this-current_page + 1;
return '?' 、http_build_query($params);
}
public function getPageUrl($page) {
$params = $this-url_params;
$params['page'] = $page;
return '?' 、http_build_query($params);
}
public function getPaginationHtml($show_numbers = true) {
$html = '<div class=pagination';
// 上一页
if ($this-hasPrev()) {
$html .= '<a href=' 、$this-getPrevPageUrl() 、'上一页</a';
}
if ($show_numbers) {
// 页码数字
$start = max(1, $this-current_page 2);
$end = min($this-total_pages, $this-current_page + 2);
if ($start 1) {
$html .= '<a href=' 、$this-getPageUrl(1) 、'1</a';
if ($start 2) {
$html .= '<span...</span';
}
}
for ($i = $start; $i <= $end; $i++) {
if ($i == $this-current_page) {
$html .= '<span class=current' 、$i 、'</span';
} else {
$html .= '<a href=' 、$this-getPageUrl($i) 、'' 、$i 、'</a';
}
}
if ($end < $this-total_pages) {
if ($end < $this-total_pages 1) {
$html .= '<span...</span';
}
$html .= '<a href=' 、$this-getPageUrl($this-total_pages) 、'' 、$this-total_pages 、'</a';
}
}
// 下一页
if ($this-hasNext()) {
$html .= '<a href=' 、$this-getNextPageUrl() 、'下一页</a';
}
$html .= '</div';
return $html;
}
public function getInfo() {
$start = ($this-current_page 1) * $this-per_page + 1;
$end = min($this-current_page * $this-per_page, $this-total_records);
return [
'current_page' = $this-current_page,
'total_pages' = $this-total_pages,
'total_records' = $this-total_records,
'per_page' = $this-per_page,
'start_record' = $start,
'end_record' = $end
];
}
}
// 使用示例
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
// 查询数据
$stmt = $pdo-prepare(SELECT COUNT(*) FROM articles WHERE title LIKE ?);
$stmt-execute([%{$search}%]);
$total_records = $stmt-fetchColumn();
$pagination = new Pagination($total_records, 10, $page);
$stmt = $pdo-prepare(SELECT * FROM articles WHERE title LIKE ? LIMIT :limit OFFSET :offset);
$stmt-bindValue(':limit', $pagination-per_page, PDO::PARAM_INT);
$stmt-bindValue(':offset', $pagination-getOffset(), PDO::PARAM_INT);
$stmt-execute([%{$search}%]);
$articles = $stmt-fetchAll(PDO::FETCH_ASSOC);
// 显示数据和分页
foreach ($articles as $article) {
echo '<div' 、htmlspecialchars($article['title']) 、'</div';
}
echo $pagination-getPaginationHtml();
?
4、安全注意事项
php
<?php
// 1、验证和过滤GET参数
$page = isset($_GET['page']) ? filter_var($_GET['page'], FILTER_VALIDATE_INT) : 1;
if ($page === false $page < 1) {
$page = 1;
}
// 2、限制每页最大记录数
$per_page = isset($_GET['per_page']) ? (int)$_GET['per_page'] : 10;
$per_page = min(100, max(1, $per_page)); // 限制在1-100之间
// 3、使用预处理语句防止SQL注入
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$stmt = $pdo-prepare(SELECT * FROM table WHERE column LIKE ?);
$stmt-execute([%{$search}%]);
// 4、XSS防护
echo htmlspecialchars($search, ENT_QUOTES, 'UTF-8');
?
这些示例展示了PHP分页中GET参数的完整使用方法,包括基础分页、带搜索条件的分页、面向对象封装以及安全考虑。根据您的具体需求选择合适的实现方式。
📣 商家广告:





0
IP地址: 251.33.180.35
搜索次数: 1
提问时间: 2025-11-04 00:50:53
🛒 域名购买:
191.net.cn
jjxs.cn
797.net.cn
826.net.cn
xg.gs
836.net.cn
kt6.cn
652.net.cn
sou.gg
jqjp.cn
❓️ 热门提问:
mini主机独显
结对编程
主机所
机房配电安装
淮安企业邮箱dns
ping请求找不到主机
vps云主机 区别
cdn加速服务搭建
美国服务器云服务器的
关键词开屏广告
豌豆Ai站群搜索引擎系统
🌐 域名评估:
usvj.cn
pai.pw
dbmm.cn
jd.com
s-k.com.cn
85.com.hk
2.pw
pai.kim
9el.cn
zz.sc.cn
⛏ 最新挖掘:
凯里酸汤鱼
贵阳烤脑花
都匀毛尖茶
花溪牛肉粉
贵阳烤生蚝
贵阳烤豆腐
贵阳纸包鱼
贵阳特色菜
🖌 热门作画:
🤝 关于我们:
豌豆Ai
域名
建站
站群
留痕
推广
评估
源码
开发Ai
工具
日记
价格
加盟
广告
流量
留言
联系
🗨 加入群聊:
🔗 友情链接:
文件上传
广州白云月饼厂
ai提问
🧰 站长工具:
Ai工具 whois查询 搜索
📢 温馨提示:本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持:本站由豌豆Ai提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。