当前位置:首页 > PHP

php分页效果实现

2026-02-14 02:51:50PHP

基本分页实现

使用 LIMITOFFSET 进行数据库查询分页。获取当前页码和每页显示数量,计算偏移量。

$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 10;
$offset = ($currentPage - 1) * $perPage;

$sql = "SELECT * FROM articles LIMIT $offset, $perPage";
$result = $conn->query($sql);

计算总页数

查询总记录数并计算总页数,用于生成分页导航。

$totalSql = "SELECT COUNT(*) as total FROM articles";
$totalResult = $conn->query($totalSql);
$totalRows = $totalResult->fetch_assoc()['total'];
$totalPages = ceil($totalRows / $perPage);

分页导航生成

创建分页链接,包含首页、上一页、页码和下一页、末页。

$baseUrl = 'http://example.com/articles.php?page=';
echo '<div class="pagination">';
if ($currentPage > 1) {
    echo '<a href="' . $baseUrl . '1">首页</a>';
    echo '<a href="' . $baseUrl . ($currentPage - 1) . '">上一页</a>';
}

for ($i = 1; $i <= $totalPages; $i++) {
    $active = $i == $currentPage ? ' class="active"' : '';
    echo '<a href="' . $baseUrl . $i . '"' . $active . '>' . $i . '</a>';
}

if ($currentPage < $totalPages) {
    echo '<a href="' . $baseUrl . ($currentPage + 1) . '">下一页</a>';
    echo '<a href="' . $baseUrl . $totalPages . '">末页</a>';
}
echo '</div>';

使用预处理语句

防止SQL注入,使用预处理语句处理分页参数。

$stmt = $conn->prepare("SELECT * FROM articles LIMIT ?, ?");
$stmt->bind_param("ii", $offset, $perPage);
$stmt->execute();
$result = $stmt->get_result();

分页样式优化

添加CSS样式美化分页导航,提高用户体验。

.pagination {
    margin: 20px 0;
    text-align: center;
}
.pagination a {
    display: inline-block;
    padding: 8px 16px;
    margin: 0 4px;
    border: 1px solid #ddd;
    text-decoration: none;
    color: #333;
}
.pagination a.active {
    background-color: #4CAF50;
    color: white;
    border: 1px solid #4CAF50;
}
.pagination a:hover:not(.active) {
    background-color: #ddd;
}

分页类封装

创建可重用的分页类,简化分页逻辑。

php分页效果实现

class Pagination {
    private $currentPage;
    private $perPage;
    private $totalRows;

    public function __construct($currentPage = 1, $perPage = 10, $totalRows = 0) {
        $this->currentPage = $currentPage;
        $this->perPage = $perPage;
        $this->totalRows = $totalRows;
    }

    public function getOffset() {
        return ($this->currentPage - 1) * $this->perPage;
    }

    public function getTotalPages() {
        return ceil($this->totalRows / $this->perPage);
    }

    public function getPaginationLinks($baseUrl) {
        $totalPages = $this->getTotalPages();
        $links = '';

        if ($this->currentPage > 1) {
            $links .= '<a href="' . $baseUrl . '1">首页</a>';
            $links .= '<a href="' . $baseUrl . ($this->currentPage - 1) . '">上一页</a>';
        }

        for ($i = 1; $i <= $totalPages; $i++) {
            $active = $i == $this->currentPage ? ' class="active"' : '';
            $links .= '<a href="' . $baseUrl . $i . '"' . $active . '>' . $i . '</a>';
        }

        if ($this->currentPage < $totalPages) {
            $links .= '<a href="' . $baseUrl . ($this->currentPage + 1) . '">下一页</a>';
            $links .= '<a href="' . $baseUrl . $totalPages . '">末页</a>';
        }

        return $links;
    }
}

标签: 分页效果
分享给朋友:

相关文章

vue实现分页功能

vue实现分页功能

Vue 分页功能实现 在 Vue 中实现分页功能通常需要结合后端 API 或前端数据分页逻辑。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在 Vue 组件中处理分页逻辑。…

vue分页实现

vue分页实现

Vue 分页实现方法 基础分页组件实现 创建一个简单的分页组件,包含上一页、下一页和页码按钮。组件接收当前页码、总页数等参数,通过事件通知父组件页码变化。 <template> &l…

jquery 分页

jquery 分页

实现 jQuery 分页的方法 客户端分页实现 使用 jQuery 实现客户端分页的核心思路是通过 JavaScript 控制数据的显示与隐藏。假设有一个包含数据的 HTML 列表或表格: // 分…

vue实现滚动效果

vue实现滚动效果

Vue 实现滚动效果的方法 使用原生滚动方法 通过 Vue 的 ref 获取 DOM 元素,调用原生 scrollTo 或 scrollIntoView 方法实现滚动。 <template&g…

js分页实现

js分页实现

分页的基本原理 分页的核心是通过计算当前页码和数据偏移量,从服务器或本地数据中截取对应范围的数据进行展示。通常需要以下参数:当前页码(currentPage)、每页条数(pageSize)、总数据量(…

vue实现翻书效果

vue实现翻书效果

Vue实现翻书效果的方法 翻书效果可以通过CSS动画和Vue的动态绑定实现,以下是几种常见的实现方式: 使用CSS transform和transition 通过CSS的transform属性实现翻…