我试图限制所显示的分页。我的网站有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();
}
}
答案 0 :(得分:8)
分页页码限制问题由chnage解决
for ($x=1; $x<=$pages; $x++)
到
for($x = max(1, $page - 5); $x <= min($page + 5, $pages); $x++)
答案 1 :(得分:7)
您对此有何看法?:
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++)
答案 2 :(得分:0)
//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();
}
答案 3 :(得分:0)
对于大量页面,请考虑使用“对数”分页显示链接。请在此处查看我的答案(包含PHP代码):
How to do page navigation for many, many pages? Logarithmic page navigation
答案 4 :(得分:0)
我尝试了wallyk和Hemang提供的答案,但他们的分页情况不足。他们的答案有时会显示比范围更少的链接。我不得不添加一些最大和最小的陈述。
这是我对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的。