douwu8251 2015-09-15 17:23
浏览 19
已采纳

分页与php限制来自get变量的链接数

I am making a simple image gallery and have hit a road block. Its simply displaying a number of images and using pagination to separate 16 images per page. This all works as does my current pagination. However my pagination bar has 101 buttons on it. I want to limit that to a range of 8 pages 4 on either side of the current page and 8 on either side if at beginning or end etc. My current_page and last_page variables are simply a get variable from the page address. Is there a simple way to accomplish this.

for ($i = 1; $i < ($last_page + 1); $i++) {
    if ($current_page == $i) {
        echo '<li class="active"><a href="?page=' . $i . '">' . $i . '</a></li>';
    } else {
        echo '<li><a href="?page=' . $i . '">' . $i . '</a></li>';
    }
}
  • 写回答

1条回答 默认 最新

  • doubu1853 2015-09-15 17:35
    关注
    $currentPage = 4;
    $numberPages = 8;
    $maxPages = 106;
    for ($i = $currentPage - ((int)($numberPages/2)), $pages = 0; $pages < $numberPages && $i <= $maxPages; $i++) {
        if ($i > 0) {
            if ($i == $currentPage) {
                echo '<li class="active"><a href="?page='.$i.
                '">'.$i.
                '</a></li>';
            } else {
                echo '<li><a href="?page='.$i.
                '">'.$i.
                '</a></li>';
            }
            $pages++;
        }
    }
    

    Something along these lines should work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?