Php 如何限制分页中显示的页码?

Php 如何限制分页中显示的页码?,php,pagination,Php,Pagination,我试图限制显示的分页。我的站点有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['paginatio

我试图限制显示的分页。我的站点有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();
    }
}
$skin=newskin('site/pagination')$分页='';
如果($pages>=1&&$pages make();
}
}

您希望它做什么

for ($x=1; $x<=$pages; $x++)
for($x=1;$x
//假设您希望在当前页面的两侧各有3页:
$skin=新皮肤('site/pagination');$pagination='';
$currentPage=以存储方式获取当前页码;
//从当前页面将下限设置为3
$fromPage=$currentPage-3;
//边界检查您呼叫的页面是否为0或负数
如果($fromPage<1){
$fromPage=1;
}
//为你想要的设定上限
$toPage=$fromPage+7;//7是您希望显示的页面数
//检查它是否没有超过您拥有的最大页数
如果($toPage>$maxPages){
$toPage=$maxPages;
}
//在你的范围内迭代
对于($x=$fromPage;$xmake();
}

分页页数限制问题通过chnage解决

for ($x=1; $x<=$pages; $x++)

<代码> >(x=1;$x< p>对于大量页面,考虑使用“对数”分页显示链接。请参阅这里的答案(包含PHP代码):


我尝试了wallyk和Hemang提供的答案,但它们与我的分页情况不符。它们的答案有时显示的链接比范围少。我必须添加一些max和min语句

以下是我对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的。

请告诉我们您的变量的含义。看起来您可以这样做,但如果您不想通过仅限制
$pages
来全局限制它,我们需要某种当前页面变量。请看这个问题:
//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();
}
for ($x=1; $x<=$pages; $x++)
for($x = max(1, $page - 5); $x <= min($page + 5, $pages); $x++)
var minPage = Math.max(Math.min(currentPage - (range / 2), totalPages - range), 0);
var maxPage = Math.min(Math.max(currentPage + (range / 2), range), totalPages);