【问题标题】:PHP Pagination - Limit amount of linksPHP分页 - 限制链接数量
【发布时间】:2013-01-22 09:54:35
【问题描述】:

所以我有这样的分页链接。

for ( $counter = 0; $counter <= $page_amount; $counter += 1) {
        echo "<a href=\"section.php?q=$section&p=$counter\">";
        echo $counter+1;
        echo "</a>";
     }

链接增长如下:

1 2 3 4 5 6 7 等等。

但我想限制这一点,所以如果页面超过 7 个,它应该只显示 7 个这样的链接:

1 2 3 ... 10 11 12

其中 12 是最后一页。

如果你转到下一页,它只会像这样更改第一页:

3 4 5 ... 10 11 12

直到您像这样到达最后 7 页:

6 7 8 9 10 11 12

我该怎么做??

请帮忙。

【问题讨论】:

  • 我第一次访问这个网站,我得到了很多帮助 =/ 耶!我想那是一次尝试..
  • 如果您希望得到帮助但又不尝试自己修复它,那么请访问其他地方。
  • 我的尝试就在我得到的范围内...
  • 使用一些数学来制作一个通用方程,然后用简单的 if 语句和循环对其进行编码

标签: php pagination hyperlink limit paging


【解决方案1】:

这是一种方法。

// Set some presets
$current_page = 0;
$page_amount = 11;
$limiter = 7;

// Set upper and lower number of links
$sides = round(($limiter/2), 0, PHP_ROUND_HALF_DOWN);

for ( $counter = 0; $counter <= $page_amount; $counter++) {
    // Start with Current Page
    if($counter >= ($current_page)){
        // Show page links of upper and lower
        if(($counter <($current_page+$sides))||($counter >($page_amount-$sides))){
            echo "<a href=\"section.php?q=$section&p=$counter\">";
            echo $counter+1;
            echo "</a> ";
        }
        // The middle link
        elseif($counter ==($current_page+$sides)){
            echo "<a href=\"page.php?p=$counter\">";
                    // Show number if number of links == $limiter
            if(($page_amount-$current_page)==$limiter-1){
                 echo $counter+1;
            }
            // Show '...' number of links > $limiter 
                    else {
                     echo "...";
            }
            echo "</a> ";
        }
     }
}

这允许更改显示的链接数量,即。从 7 点到 9 点。

注意,在round() 中使用PHP_ROUND_HALF_DOWN 需要php>=5.3

【讨论】:

    猜你喜欢
    • 2021-11-02
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 2015-12-12
    • 2015-11-07
    • 2014-09-26
    • 2011-07-09
    相关资源
    最近更新 更多