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

使用分页类

封装分页逻辑为可重用类

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

php分页代码实现

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

相关文章

h5实现响应式代码

h5实现响应式代码

响应式设计基础 HTML5实现响应式设计主要通过媒体查询(Media Queries)、弹性布局(Flexbox)、网格布局(Grid)和视口设置(Viewport Meta Tag)等技术。核心目标…

vue实现分页条数

vue实现分页条数

Vue 实现分页条数控制 在 Vue 中实现分页条数控制通常需要结合分页组件和后端 API 支持。以下是几种常见的实现方式: 使用 Element UI 的分页组件 Element UI 提供了强大…

react如何代码优化

react如何代码优化

减少不必要的重新渲染 使用 React.memo 包装函数组件以避免在 props 未变化时重新渲染。对于类组件,可以通过 shouldComponentUpdate 或继承 PureComponen…

jquery 分页

jquery 分页

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

vue怎样实现分页

vue怎样实现分页

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

vue实现滚动分页

vue实现滚动分页

实现滚动分页的基本思路 滚动分页(Infinite Scroll)是一种常见的前端分页加载方式,当用户滚动到页面底部时自动加载下一页数据。Vue 结合现代前端工具可以轻松实现这一功能。 监听滚动事…