当前位置:首页 > PHP

php分页代码实现

2026-02-27 23:16:53PHP

基本分页实现

使用LIMITOFFSET进行数据库查询分页

$perPage = 10; // 每页显示数量
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1; // 当前页码
$offset = ($page - 1) * $perPage; // 计算偏移量

// 查询数据
$sql = "SELECT * FROM your_table LIMIT {$offset}, {$perPage}";
$result = $conn->query($sql);

// 获取总记录数
$totalSql = "SELECT COUNT(*) as total FROM your_table";
$totalResult = $conn->query($totalSql);
$totalRow = $totalResult->fetch_assoc();
$total = $totalRow['total'];

// 计算总页数
$totalPages = ceil($total / $perPage);

分页链接生成

创建分页导航HTML

$url = $_SERVER['PHP_SELF'] . '?'; // 当前页面URL
$query = $_GET;
unset($query['page']); // 移除原有page参数

if (!empty($query)) {
    $url .= http_build_query($query) . '&';
}

// 生成分页链接
echo '<div class="pagination">';
if ($page > 1) {
    echo '<a href="' . $url . 'page=' . ($page - 1) . '">上一页</a>';
}

for ($i = 1; $i <= $totalPages; $i++) {
    if ($i == $page) {
        echo '<span class="current">' . $i . '</span>';
    } else {
        echo '<a href="' . $url . 'page=' . $i . '">' . $i . '</a>';
    }
}

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

优化分页查询

使用预处理语句防止SQL注入

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

大数据量分页优化

使用"上一页/下一页"模式替代传统分页

$nextPage = $page + 1;
$prevPage = $page - 1;

echo '<div class="pagination">';
if ($prevPage > 0) {
    echo '<a href="' . $url . 'page=' . $prevPage . '">上一页</a>';
}
if ($nextPage <= $totalPages) {
    echo '<a href="' . $url . 'page=' . $nextPage . '">下一页</a>';
}
echo '</div>';

使用分页类

封装分页逻辑为可重用类

php分页代码实现

class Pagination {
    private $total;
    private $perPage;
    private $currentPage;
    private $url;

    public function __construct($total, $perPage, $currentPage, $url) {
        $this->total = $total;
        $this->perPage = $perPage;
        $this->currentPage = $currentPage;
        $this->url = $url;
    }

    public function getLinks() {
        $totalPages = ceil($this->total / $this->perPage);
        $html = '<div class="pagination">';

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

        for ($i = 1; $i <= $totalPages; $i++) {
            if ($i == $this->currentPage) {
                $html .= '<span class="current">' . $i . '</span>';
            } else {
                $html .= '<a href="' . $this->url . $i . '">' . $i . '</a>';
            }
        }

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

        $html .= '</div>';
        return $html;
    }
}

标签: 分页代码
分享给朋友:

相关文章

vue实现分页功能

vue实现分页功能

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

vue搜索过后实现分页

vue搜索过后实现分页

实现 Vue 搜索后分页的方法 数据绑定与搜索逻辑 在 Vue 组件中定义数据属性,包括原始数据列表、搜索关键词、分页相关参数: data() { return { originalLi…

vue 分页 实现

vue 分页 实现

Vue 分页实现方法 使用 Element UI 分页组件 Element UI 提供了现成的分页组件 el-pagination,适合快速集成。需要先安装 Element UI 库。 安装 Ele…

淘宝css代码制作

淘宝css代码制作

在淘宝店铺装修中,CSS代码用于自定义页面样式,提升视觉体验。以下是关键方法和注意事项: 自定义模块样式 通过淘宝旺铺的“自定义模块”功能,可以插入CSS代码。代码需包裹在<style>…

vue怎样实现分页

vue怎样实现分页

Vue实现分页的方法 使用第三方库(如Element UI或Ant Design Vue) Element UI的Pagination组件可以直接集成到Vue项目中。安装Element UI后,在组件…

vue实现分页加载

vue实现分页加载

Vue 分页加载的实现方法 基础分页实现(后端分页) 安装依赖(如使用axios): npm install axios 示例代码: <template> <div…