当前位置:首页 > 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中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方式: 使用props传递数据 父组件通过props向子组件传递当前页码和总页数等分页信息:…

uniapp 分页组件

uniapp 分页组件

uniapp 分页组件实现方法 在uniapp中实现分页功能,可以通过自定义组件或使用第三方组件库完成。以下是几种常见实现方式: 自定义分页组件 创建一个名为uni-pagination的组件,模板…

js分页实现

js分页实现

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

前端vue实现分页

前端vue实现分页

前端Vue实现分页的方法 在Vue中实现分页功能可以通过多种方式完成,以下是常见的实现方法: 使用Element UI的分页组件 Element UI提供了现成的分页组件el-pagination,…

vue实现动画代码

vue实现动画代码

Vue 动画实现方法 Vue 提供了多种方式实现动画效果,包括内置过渡组件、CSS 动画库集成以及第三方动画库结合。以下是几种常见实现方法: 使用 Vue 内置过渡组件 Vue 的 <tran…

vue 实现表格分页

vue 实现表格分页

Vue 实现表格分页的方法 使用 Element UI 的分页组件 Element UI 提供了现成的分页组件 el-pagination,结合 el-table 可以快速实现表格分页功能。 安装…