0

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

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

我该怎么做呢 ??

请帮忙。

4

1 回答 1

0

这是一种方法。

// 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 点。

注意,使用PHP_ROUND_HALF_DOWNinround()需要 php>=5.3

于 2013-02-07T04:03:02.867 回答