【问题标题】:How can I limit the page numbers shown in the pagination?如何限制分页中显示的页码?
【发布时间】:2012-07-06 18:18:25
【问题描述】:

我正在尝试限制显示的分页。我的网站有 500 多个页面,所有 500 多个数字都显示在分页中。

我想像这样限制它:

Prev 1 2 3 4 5 6 Next 

我的代码:

$skin = new skin('site/pagination'); $pagination = '';
if ($pages >= 1 && $page <= $pages) {
    for ($x=1; $x<=$pages; $x++) {
        $TMPL['pagination'] = ($x == $page) ? '<strong><a href="/writer/'.urlencode($name).'/'.$x.'">'.$x.'</a></strong> ' : '<a href="/writer/'.urlencode($name).'/'.$x.'">'.$x.'</a> ';
        $pagination .= $skin->make();
    }
}

【问题讨论】:

  • 请告诉我们您的变量的含义。看起来你可以做到,但如果你不想通过限制 $pages 来全局限制它,我们需要某种当前页面变量
  • 看到这个问题:stackoverflow.com/q/7835752/999120>

标签: php pagination


【解决方案1】:

chnage解决的分页页数限制问题

for ($x=1; $x<=$pages; $x++)

for($x = max(1, $page - 5); $x <= min($page + 5, $pages); $x++)

【讨论】:

    【解决方案2】:

    你希望它做什么?:

    for ($x=1; $x<=$pages; $x++)
    

    它将为每个页面创建一个条目。如果您不希望这样,请限制它的意义:

    for ($x=1; $x <= min(5, $pages); $x++)
    

    最好考虑当前页面:

    for ($x=max($curpage-5, 1); $x<=max(1, min($pages,$curpage+5)); $x++)
    

    【讨论】:

      【解决方案3】:
      //Let's say you want 3 pages on either side of your current page:
      
      $skin = new skin('site/pagination'); $pagination = '';
      $currentPage = get the current page number however you have it stored;
      
      // set the lower bound as 3 from the current page
      $fromPage = $currentPage - 3;
      
      // bounds check that you're not calling for 0 or negative number pages
      if($fromPage < 1) {
          $fromPage = 1;
      }
      
      // set the upper bound for what you want
      $toPage = $fromPage + 7; // 7 is how many pages you'd like shown
      
      // check that it doesn't exceed the maximum number of pages you have
      if($toPage > $maxPages) {
          $toPage = $maxPages;
      }
      
      // iterate over your range
      for ($x=$fromPage; $x<=$toPage; $x++) {
          $TMPL['pagination'] = ($x == $page) ? '<strong><a href="/writer/'.urlencode($name).'/'.$x.'">'.$x.'</a></strong> ' : '<a href="/writer/'.urlencode($name).'/'.$x.'">'.$x.'</a> ';
          $pagination .= $skin->make();
      }
      

      【讨论】:

        【解决方案4】:

        对于大量页面,请考虑使用“对数”分页显示链接。在此处查看我的答案(包括 PHP 代码):

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

        【讨论】:

          【解决方案5】:

          我尝试了 wallyk 和 Hemang 提供的答案,但它们无法满足我的分页案例。他们的答案有时会显示比范围更少的链接。我不得不添加一些 max 和 min 语句。

          这是我对 Javascript 代码的看法:

          var minPage = Math.max(Math.min(currentPage - (range / 2), totalPages - range), 0);
          var maxPage = Math.min(Math.max(currentPage + (range / 2), range), totalPages);
          

          范围是始终显示的链接数。 totalPages 是要迭代的总页数。 currentPage 是当前显示的页面。

          请注意,我的分页索引是从 0 开始的。

          【讨论】:

            猜你喜欢
            • 2021-05-16
            • 1970-01-01
            • 2018-04-19
            • 2011-12-23
            • 2020-02-14
            • 2014-10-14
            • 2015-07-20
            • 2020-03-11
            • 1970-01-01
            相关资源
            最近更新 更多