所以我有这样的分页链接。
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
我该怎么做?
请帮忙。
答案 0 :(得分: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_DOWN
中使用round()
需要php&gt; = 5.3