所以我有这样的分页链接。
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
我该怎么做??
请帮忙。
这是一种方法。
// 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>=5.3