php实现分页类
PHP 分页类实现
以下是一个简单的 PHP 分页类实现示例,包含基本的分页逻辑和样式控制:
<?php
class Pagination {
private $totalItems;
private $itemsPerPage;
private $currentPage;
private $totalPages;
private $pageRange = 5;
private $url;
public function __construct($totalItems, $itemsPerPage, $currentPage, $url) {
$this->totalItems = $totalItems;
$this->itemsPerPage = $itemsPerPage;
$this->currentPage = $currentPage;
$this->url = $url;
$this->totalPages = ceil($totalItems / $itemsPerPage);
}
public function getPagination() {
if ($this->totalPages <= 1) {
return '';
}
$pagination = '<ul class="pagination">';
// 上一页
if ($this->currentPage > 1) {
$pagination .= '<li><a href="' . $this->url . ($this->currentPage - 1) . '">«</a></li>';
}
// 页码
$start = max(1, $this->currentPage - floor($this->pageRange / 2));
$end = min($this->totalPages, $start + $this->pageRange - 1);
if ($end - $start < $this->pageRange - 1) {
$start = max(1, $end - $this->pageRange + 1);
}
if ($start > 1) {
$pagination .= '<li><a href="' . $this->url . '1">1</a></li>';
if ($start > 2) {
$pagination .= '<li class="disabled"><span>...</span></li>';
}
}
for ($i = $start; $i <= $end; $i++) {
if ($i == $this->currentPage) {
$pagination .= '<li class="active"><span>' . $i . '</span></li>';
} else {
$pagination .= '<li><a href="' . $this->url . $i . '">' . $i . '</a></li>';
}
}
if ($end < $this->totalPages) {
if ($end < $this->totalPages - 1) {
$pagination .= '<li class="disabled"><span>...</span></li>';
}
$pagination .= '<li><a href="' . $this->url . $this->totalPages . '">' . $this->totalPages . '</a></li>';
}
// 下一页
if ($this->currentPage < $this->totalPages) {
$pagination .= '<li><a href="' . $this->url . ($this->currentPage + 1) . '">»</a></li>';
}
$pagination .= '</ul>';
return $pagination;
}
}
?>
使用示例
<?php
// 假设总记录数为100,每页显示10条,当前页码从URL获取
$totalItems = 100;
$itemsPerPage = 10;
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$url = 'index.php?page=';
$pagination = new Pagination($totalItems, $itemsPerPage, $currentPage, $url);
echo $pagination->getPagination();
?>
数据库查询配合分页
<?php
// 数据库查询示例
$limit = $itemsPerPage;
$offset = ($currentPage - 1) * $itemsPerPage;
// 假设使用PDO
$stmt = $pdo->prepare("SELECT * FROM your_table LIMIT :limit OFFSET :offset");
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 显示数据
foreach ($results as $row) {
// 显示数据内容
}
// 显示分页
echo $pagination->getPagination();
?>
分页样式建议
可以添加以下CSS样式来美化分页:
.pagination {
display: inline-block;
padding: 0;
margin: 20px 0;
}
.pagination li {
display: inline;
margin: 0 5px;
}
.pagination li a, .pagination li span {
padding: 8px 16px;
text-decoration: none;
border: 1px solid #ddd;
color: #337ab7;
}
.pagination li.active span {
background-color: #337ab7;
color: white;
border: 1px solid #337ab7;
}
.pagination li a:hover {
background-color: #eee;
}
高级功能扩展
如果需要更复杂的功能,可以考虑以下扩展:
- 添加AJAX支持,实现无刷新分页
- 支持自定义URL参数格式
- 添加每页显示数量选择器
- 支持多语言
- 添加跳转到指定页码功能
这个分页类提供了基本的分页功能,可以根据项目需求进行修改和扩展。







