【问题标题】:Truncate number of pages in pagination截断分页中的页数
【发布时间】:2013-01-07 23:37:44
【问题描述】:

这可能是一个非常愚蠢的问题,但我想不出任何可以帮助我走得更远的东西。我希望在我的页面导航中缩短 NUMBERS 的数量。

而不是像:1、2、3、4、5、6、7、8

我希望它像:1, 2...7, 8

当我转到 2 时,现在应该在数字组中看到数字 3

这是我负责页码的代码:

<div id="user_nav_bottom">
<?php for($i=1; $i < sizeof($pages)+1; $i++) {
    $strong = ($_GET['page'] == $i) ? ' dark bold' : '';
    echo '<span class="normal' . $strong . '"><a href="imgit_images.php?page=' . $i . '">' . $i . ', </a></span>';
} ?>
</div>

【问题讨论】:

  • 你到底有什么问题?成为会员近 2 年,问了近 50 个问题,你现在应该知道如何问一个好问题了。
  • 是的,但我往往会经常被简单的东西阻塞,而且无论我多么努力地想出一个解决方案,我自己都无法得到它,所以我在这里问。我觉得我把我的问题描述得很好,你不明白的地方告诉我?
  • 我不明白您遇到了什么问题。您不知道如何截断页码,但您究竟坚持的是什么?
  • 我无法为我的分页菜单制作一小组数字。有很多结果,这意味着很多页面。页面数量正在破坏我的网站(在我的网站中添加一个水平滚动条),我需要通过缩短页面数量并用那些小点替换它们来解决这个问题,或者检查谷歌,搜索一些东西并转到底部在页面中,您会看到仅显示数字 1-10,但是当您转到 10 时,您将获得 5-15。现在找我?
  • 1000页看起来怎么样?我希望不是这样:1, 2...999, 1000.

标签: php pagination


【解决方案1】:

这是我为分页论坛编写的一段修改过的代码。

它的输出与您显示的不完全一样,而应该只是调整的问题。

<?php 
    //Set up vars

    $currentPage = isset($_GET["page"])? $_GET["page"] : 1;
    $numPages = count($pages);
    $numPages = 7;//cause I don't have the real var for testing

    $howMany = 1;
?>


<?php if ($numPages > 1): ?>
    <li>Page <?php echo $currentPage?> of <?php echo $numPages?></li>
    <?php if ($currentPage > 1): ?>
        <li><a href="imgit_images.php?page=1">First</a></li>
        <li><a href="imgit_images.php?page=<?php echo $currentPage-1?>">&lt;</a></li>
    <?php endif; ?>

    <?php if ($currentPage > $howMany + 1): ?>
        <li>...</li>
    <?php endif; ?>

    <?php for ($pageIndex = $currentPage - $howMany; $pageIndex <= $currentPage + $howMany; $pageIndex++): ?>
        <?php if ($pageIndex >= 1 && $pageIndex <= $numPages): ?>
            <li>
                <?php if ($pageIndex == $currentPage): ?>
                    <u>
                <?php endif; ?>

                <a href="imgit_images.php?page=<?php echo $pageIndex?>"><?php echo $pageIndex?></a>

                <?php if ($pageIndex == $currentPage): ?>
                    </u>
                <?php endif; ?>
            </li>
        <?php endif; ?>
    <?php endfor; ?>

    <?php if ($currentPage < $numPages - $howMany): ?>
        <li>...</li>
    <?php endif; ?>

    <?php if ($currentPage < $numPages): ?>
        <li><a href="imgit_images.php?page=<?php echo $currentPage+1?>">&gt;</a></li>
        <li><a href="imgit_images.php?page=-1">Last</a></li>
    <?php endif; ?>
<?php endif; ?>

【讨论】:

    【解决方案2】:

    看看这个:https://codereview.stackexchange.com/a/10292。此解决方案应该可以解决您的问题。我认为这是一个非常好的方法。

    【讨论】:

      【解决方案3】:

      这个显示了当前请求的链接之前的两个链接和之后的两个链接:

      <div id="user_nav_bottom">
      <?php
      $current = $_GET['page'];
      $last = count($pages)+1;
      $curr0 = $current-2;
      $curr1 = $current+2;
      if ($curr0<=1) {
        $curr0 = 1;
        $curr1 = $last>5? 5 : $last;
      }
      if ($curr1>=$last) {
        $curr0 = $last-4 < 1 ? 1 : $last-4;
        $curr1 = $last;
      }
      // now print all links:
      echo '<a href="imgit_images.php?page=1">&#171;</a> ';
      for ($i=$curr0; $i<=$curr1; $i++) {
        $style = ($i==$current)? 'font-weight:bold':'';
        echo ' <a href="imgit_images.php?page='.$i.'" style="'.$style.'">'.$i.'</a> ';
      }
      echo '<a href="imgit_images.php?page='.$last.'">&#187;</a> ';
      ?>
      </div>
      

      这显示如下链接:« 13 14 15 16 17 »
      恕我直言,通过添加适当的条件来添加前五个和后五个链接并不难。

      Testing script here

      【讨论】:

        【解决方案4】:

        您可能想要做的是使用我在此处的回答中描述的“对数”分页技术:

        How to do page navigation for many, many pages? Logarithmic page navigation

        如果将 LINKS_PER_STEP 设置为 2 或 3 之类的低值,则会产生非常紧凑的输出。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多