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

计算总页数

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

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

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

php分页效果实现

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

相关文章

vue router实现分页

vue router实现分页

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

jquery 分页

jquery 分页

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

uniapp 卡片效果

uniapp 卡片效果

uniapp 实现卡片效果的方法 使用 view 和 CSS 样式 通过 view 组件结合 CSS 样式可以快速实现卡片效果。设置圆角、阴影和边距来增强视觉层次感。 <view class…

vue elementui实现分页

vue elementui实现分页

Vue + ElementUI 分页实现方法 安装与引入 确保已安装 ElementUI,可通过 npm 或 yarn 安装: npm install element-ui 在 main.js 中…

vue效果实现

vue效果实现

Vue 效果实现方法 Vue.js 提供了多种方式实现动态效果,包括过渡动画、状态驱动的 UI 变化以及第三方动画库集成。以下是常见效果的实现方案: 过渡动画 使用 Vue 内置的 <tran…

react如何实现分页

react如何实现分页

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