php实现搜索分页
PHP实现搜索分页的方法
在PHP中实现搜索分页功能需要结合数据库查询和分页逻辑处理。以下是具体实现步骤:
获取搜索参数并构建查询
从GET或POST请求中获取搜索关键词和其他参数,构建SQL查询条件。使用预处理语句防止SQL注入。
$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : '';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 10; // 每页显示数量
// 构建查询条件
$where = '';
if (!empty($keyword)) {
$where = "WHERE title LIKE :keyword OR content LIKE :keyword";
}
计算总记录数和总页数
执行查询获取匹配搜索条件的总记录数,计算总页数。
// 查询总记录数
$countSql = "SELECT COUNT(*) FROM articles $where";
$stmt = $pdo->prepare($countSql);
if (!empty($keyword)) {
$stmt->bindValue(':keyword', "%$keyword%");
}
$stmt->execute();
$total = $stmt->fetchColumn();
// 计算总页数
$totalPages = ceil($total / $perPage);
获取当前页数据
构建分页查询,使用LIMIT子句限制返回的记录范围。
$offset = ($page - 1) * $perPage;
$sql = "SELECT * FROM articles $where ORDER BY id DESC LIMIT $offset, $perPage";
$stmt = $pdo->prepare($sql);
if (!empty($keyword)) {
$stmt->bindValue(':keyword', "%$keyword%");
}
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
生成分页链接
创建分页导航链接,保持搜索参数在分页时的传递。
$pagination = '';
for ($i = 1; $i <= $totalPages; $i++) {
$active = ($i == $page) ? 'active' : '';
$pagination .= "<li class='$active'><a href='search.php?keyword=$keyword&page=$i'>$i</a></li>";
}
前端展示
在HTML中展示搜索结果和分页导航。
<!-- 搜索结果 -->
<?php foreach ($results as $row): ?>
<div class="result-item">
<h3><?= htmlspecialchars($row['title']) ?></h3>
<p><?= htmlspecialchars($row['content']) ?></p>
</div>
<?php endforeach; ?>
<!-- 分页导航 -->
<ul class="pagination">
<?= $pagination ?>
</ul>
完整示例代码
将上述各部分组合成完整的搜索分页功能实现。
<?php
// 数据库连接
$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
// 获取参数
$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : '';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 10;
// 构建查询条件
$where = '';
if (!empty($keyword)) {
$where = "WHERE title LIKE :keyword OR content LIKE :keyword";
}
// 获取总记录数
$countSql = "SELECT COUNT(*) FROM articles $where";
$stmt = $pdo->prepare($countSql);
if (!empty($keyword)) {
$stmt->bindValue(':keyword', "%$keyword%");
}
$stmt->execute();
$total = $stmt->fetchColumn();
$totalPages = ceil($total / $perPage);
// 获取当前页数据
$offset = ($page - 1) * $perPage;
$sql = "SELECT * FROM articles $where ORDER BY id DESC LIMIT $offset, $perPage";
$stmt = $pdo->prepare($sql);
if (!empty($keyword)) {
$stmt->bindValue(':keyword', "%$keyword%");
}
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!-- 搜索表单 -->
<form method="get" action="search.php">
<input type="text" name="keyword" value="<?= htmlspecialchars($keyword) ?>">
<button type="submit">搜索</button>
</form>
<!-- 搜索结果 -->
<?php foreach ($results as $row): ?>
<div class="result-item">
<h3><?= htmlspecialchars($row['title']) ?></h3>
<p><?= htmlspecialchars($row['content']) ?></p>
</div>
<?php endforeach; ?>
<!-- 分页导航 -->
<ul class="pagination">
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
<li <?= ($i == $page) ? 'class="active"' : '' ?>>
<a href="search.php?keyword=<?= urlencode($keyword) ?>&page=<?= $i ?>"><?= $i ?></a>
</li>
<?php endfor; ?>
</ul>
注意事项
- 使用预处理语句防止SQL注入攻击
- 对输出内容进行HTML转义(htmlspecialchars)
- 对URL参数进行编码(urlencode)
- 处理页码超出范围的情况
- 考虑添加"上一页"和"下一页"按钮
- 可以添加跳转到指定页码的输入框
- 对于大数据量考虑使用缓存优化性能







