You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现选中页尽可能居中的5页码分页功能?

分页功能修复:选中页居中显示异常问题

问题描述

实现选中页尽可能居中的分页功能时出现以下异常:

  • 前2页显示正常,但点击第6页时会显示不存在的第8页;
  • 点击第7页时,第7页的选中标记消失;
  • 中间页码的选中页可正常居中显示。

问题根源

  1. 页码边界计算逻辑错误:当当前页码接近最后一页时,原代码的起始页码计算会导致范围溢出,且循环条件i < endPage会漏掉最后一页;
  2. 选中状态HTML结构不一致:选中页的<b>标签包裹<a>的结构,与非选中页的<a>标签结构冲突,导致选中标记异常;
  3. 存在未闭合的HTML标签:列表生成时的<a>标签未闭合,可能引发页面渲染异常。

修复后的完整代码

<?php
include 'backend_connection.php';
$sql = "SELECT count(`id`) FROM `activity`";
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
$totalRows = $row[0];
$page_rows = 5;
$lastPage = ceil($totalRows/$page_rows);
if($lastPage < 1){
    $lastPage = 1;
}

if(isset($_GET['page_no'])){
    $currPage = preg_replace('[^0-9]', '', $_GET['page_no']);
} else {
 $currPage = 1; 
}
// 确保当前页码在有效范围内
$currPage = max(1, min((int)$currPage, $lastPage));

$limit = 'LIMIT ' .($currPage - 1) * $page_rows .',' .$page_rows;

$sql = "SELECT `id`, `name` FROM `activity` ORDER BY name $limit";
$query = mysqli_query($con, $sql);

$paginationCtrls = '';
if($lastPage != 1){
    if ($lastPage <= 5) {
        // 总页数≤5时,显示所有页码
        for($i=1;$i<=$lastPage;$i++) {
            if ($i == $currPage)
                $paginationCtrls .= '<a href="#" style="font-weight:bold;">'.$i.'</a> &nbsp; ';
            else
                $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?page_no='.$i.'">'.$i.'</a> &nbsp; ';
        }
    } else {
        // 计算居中显示的页码范围
        $visiblePages = 5;
        $half = floor($visiblePages / 2);
        $firstPage = $currPage - $half;
        $endPage = $currPage + $half;

        // 调整起始页码,避免小于1
        if ($firstPage < 1) {
            $endPage += abs($firstPage) + 1;
            $firstPage = 1;
        }
        // 调整结束页码,避免大于最后一页
        if ($endPage > $lastPage) {
            $firstPage -= ($endPage - $lastPage);
            $endPage = $lastPage;
            $firstPage = max($firstPage, 1);
        }

        // 生成页码链接
        for($i=$firstPage;$i<=$endPage;$i++) {
            if ($i == $currPage)
                $paginationCtrls .= '<a href="#" style="font-weight:bold;">'.$i.'</a> &nbsp; ';
            else
                $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?page_no='.$i.'">'.$i.'</a> &nbsp; ';
        }
    }
}

$list = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
    $id = $row["id"];
    $name = $row["name"];
    $list .= '<p><a href="details.php?id='.$id.'">'.$name.'</a></p>';
}
mysqli_close($con);
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div#pagination_controls { font-size:21px; }
div#pagination_controls > a{ color:#06F; min-width: 70px; display:inline-block; text-decoration:none; }
div#pagination_controls > a:visited{ color:#06F; }
div#pagination_controls > a[href="#"]{ color:#333; }
</style>
</head>
<body>
<div>
  <h2><?php echo 'Total rows '.$totalRows; ?></h2>
  <p><?php echo 'Pages '.$currPage.' of '.$lastPage; ?></p>
  <p><?php echo $list; ?></p>
  <div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>
</body>
</html>

关键修复说明

  1. 优化页码范围计算:
    • 用$visiblePages控制显示的页码数量,通过$half值确保选中页居中;
    • 分别处理起始页码小于1、结束页码大于最后一页的情况,保证所有显示的页码都有效。
  2. 统一选中状态结构:
    • 用font-weight:bold样式标记选中页,保持与非选中页的<a>标签结构一致,避免样式冲突;
  3. 修复HTML结构错误:补全列表中未闭合的<a>标签;
  4. 强化页码有效性校验:将当前页码转为整数后,用max和min确保其在1到最后一页的范围内。

内容的提问来源于stack exchange,提问作者www.friend0.in

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 20:27:30