当前位置:首页 > PHP

php实现分页

2026-01-12 12:42:08PHP

PHP实现分页的基本方法

使用LIMITOFFSET进行数据库查询分页。通过计算当前页码和每页显示数量,动态生成SQL语句。

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

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

计算总页数

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

php实现分页

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

生成分页导航链接

创建分页导航HTML,包含页码链接和上一页/下一页按钮。

echo '<div class="pagination">';
if ($page > 1) {
    echo '<a href="?page='.($page - 1).'">上一页</a>';
}

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

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

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

改进查询方式,使用预处理语句提高安全性。

php实现分页

$stmt = $conn->prepare("SELECT * FROM table_name 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 $page;
    private $perPage;
    private $total;

    public function __construct($total, $perPage = 10) {
        $this->page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
        $this->perPage = $perPage;
        $this->total = $total;
    }

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

    public function getLimit() {
        return $this->perPage;
    }

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

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

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

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

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

标签: 分页php
分享给朋友:

相关文章

vue组件传值实现分页

vue组件传值实现分页

Vue 组件传值实现分页 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方法: 使用props和$emit 父组件通过props向子组件传递数据,子组件通过$emit向…

vue 分页 实现

vue 分页 实现

Vue 分页实现方法 使用第三方库(如 Element UI) Element UI 提供了现成的分页组件 el-pagination,适合快速集成。 安装 Element UI: npm ins…

vue分页实现

vue分页实现

实现Vue分页的几种方法 使用Element UI的分页组件 安装Element UI库后,可以直接使用其分页组件。以下是一个基本示例: <template> <div>…

vue  实现分页

vue 实现分页

实现分页的基本思路 在Vue中实现分页通常需要结合后端API返回的数据和前端的分页组件。分页的核心逻辑包括计算总页数、处理当前页码变化以及触发数据请求。 分页组件示例 使用Element UI的分页…

vue router实现分页

vue router实现分页

Vue Router 实现分页的方法 在 Vue.js 中,可以通过 Vue Router 实现分页功能,通常需要结合路由参数和动态路由匹配。以下是几种常见的实现方式: 使用查询参数实现分页 在路由…

vue实现页面分页

vue实现页面分页

Vue实现页面分页的方法 使用Element UI的分页组件 Element UI提供了现成的分页组件el-pagination,可以快速实现分页功能。需要先安装Element UI库。 <t…