当前位置:首页 > 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);

计算总页数

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

php分页效果实现

$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注入,使用预处理语句处理分页参数。

php分页效果实现

$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;
}

分页类封装

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

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;
    }
}

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

相关文章

react如何实现分页

react如何实现分页

实现分页的基本思路 在React中实现分页通常需要以下几个核心步骤:管理当前页码状态、计算分页数据、渲染分页控件。以下是一个典型的分页实现方法。 管理分页状态 使用React的useState钩子来…

vue分页插件实现

vue分页插件实现

Vue 分页插件实现方法 使用现成组件库(推荐) Element UI、Ant Design Vue 等主流库已提供成熟分页组件,可直接集成: <template> <…

vue实现检索效果

vue实现检索效果

Vue实现检索功能的方法 在Vue中实现检索功能通常需要结合数据绑定、计算属性和方法。以下是几种常见的实现方式: 基础实现(基于计算属性) <template> <div>…

vue实现前端分页

vue实现前端分页

实现前端分页的方法 在Vue中实现前端分页通常需要结合数据截取和分页组件。以下是常见的实现方式: 基础实现方案 数据准备 定义总数据数组和分页相关变量: data() { return {…

vue实现搜索分页

vue实现搜索分页

实现搜索分页的基本思路 在Vue中实现搜索分页功能通常需要结合以下几个关键点:数据绑定、搜索过滤、分页逻辑处理。以下是一个完整的实现方案。 数据准备与绑定 定义数据列表和分页相关变量,通常包括当前页…

vue实现分页按钮

vue实现分页按钮

实现分页按钮的基本思路 在Vue中实现分页按钮通常需要结合计算属性、事件绑定和动态样式。核心逻辑包括计算总页数、生成页码数组、处理页码切换事件以及高亮当前页码。 基础实现代码示例 <templ…