利用php实现分页
实现分页功能
分页功能通常需要结合数据库查询和前端展示。以下是一个基于PHP和MySQL的分页实现方法:
获取总记录数
计算数据库中符合条件的总记录数,用于确定总页数:
$sql = "SELECT COUNT(*) as total FROM your_table";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$total_records = $row['total'];
计算分页参数
根据当前页码和每页显示数量计算分页参数:
$records_per_page = 10;
$total_pages = ceil($total_records / $records_per_page);
$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($current_page < 1) $current_page = 1;
if ($current_page > $total_pages) $current_page = $total_pages;
$offset = ($current_page - 1) * $records_per_page;
查询当前页数据
使用LIMIT子句获取当前页的数据:
$sql = "SELECT * FROM your_table LIMIT $offset, $records_per_page";
$result = $conn->query($sql);
显示分页导航
生成分页导航链接:
echo '<div class="pagination">';
if ($current_page > 1) {
echo '<a href="?page='.($current_page-1).'">Previous</a>';
}
for ($i = 1; $i <= $total_pages; $i++) {
if ($i == $current_page) {
echo '<span class="current">'.$i.'</span>';
} else {
echo '<a href="?page='.$i.'">'.$i.'</a>';
}
}
if ($current_page < $total_pages) {
echo '<a href="?page='.($current_page+1).'">Next</a>';
}
echo '</div>';
完整示例代码
<?php
// 数据库连接
$conn = new mysqli("localhost", "username", "password", "database");
// 获取总记录数
$sql = "SELECT COUNT(*) as total FROM products";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$total_records = $row['total'];
// 分页参数
$records_per_page = 5;
$total_pages = ceil($total_records / $records_per_page);
$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($current_page < 1) $current_page = 1;
if ($current_page > $total_pages) $current_page = $total_pages;
$offset = ($current_page - 1) * $records_per_page;
// 获取当前页数据
$sql = "SELECT * FROM products LIMIT $offset, $records_per_page";
$result = $conn->query($sql);
// 显示数据
while ($row = $result->fetch_assoc()) {
echo "<div>{$row['product_name']}</div>";
}
// 分页导航
echo '<div class="pagination">';
if ($current_page > 1) {
echo '<a href="?page='.($current_page-1).'">Previous</a>';
}
for ($i = 1; $i <= $total_pages; $i++) {
if ($i == $current_page) {
echo '<span class="current">'.$i.'</span>';
} else {
echo '<a href="?page='.$i.'">'.$i.'</a>';
}
}
if ($current_page < $total_pages) {
echo '<a href="?page='.($current_page+1).'">Next</a>';
}
echo '</div>';
$conn->close();
?>
使用预处理语句的安全版本
为防止SQL注入,建议使用预处理语句:
// 获取总记录数
$stmt = $conn->prepare("SELECT COUNT(*) as total FROM products");
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$total_records = $row['total'];
// 获取当前页数据
$stmt = $conn->prepare("SELECT * FROM products LIMIT ?, ?");
$stmt->bind_param("ii", $offset, $records_per_page);
$stmt->execute();
$result = $stmt->get_result();
分页样式建议
可以添加CSS美化分页导航:
.pagination {
margin-top: 20px;
}
.pagination a, .pagination span {
padding: 8px 16px;
margin: 0 4px;
border: 1px solid #ddd;
text-decoration: none;
color: #333;
}
.pagination a:hover {
background-color: #f5f5f5;
}
.pagination .current {
background-color: #4CAF50;
color: white;
border: 1px solid #4CAF50;
}






