接下来,Prev和之间的数字

时间:2015-02-27 04:37:44

标签: php jquery mysql

嘿所以我尝试调整我从stackoverflow获得的代码,我搜索并搜索并找到解释它的视频,我能够"调整"它更多,但我不理解它,如图所示,当我试图让它显示更多的相邻数字时,它显示-1,-2或50,51,当我只有49页。

This is what I get

我试图让它显示4个相邻的数字,如下图所示。 我真的无法绕过代码。我改变了if和其他所有,如果一次只有一个,但仍然没有运气。欢迎任何意见:)

enter image description here

下面是未改变的代码,我添加了变量$ sub3,4,5 ..和$ add3,4,5 ......这就是图像的结果。 很多帖子都包含此代码,因此我不知道原始代码来自谁...... Source?

$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($pn == $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
} else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}

1 个答案:

答案 0 :(得分:1)

好的,如果我简化任务而不是弄清楚你的错误?假设$ pn是当前页面而$ last = 49(在您的示例中):

$start = max(1, $pn - 4);    // don't want negative or zero
$end = min($pn + 4, $last);  // don't want to go past 49
foreach($range($start, $end) as $n)
{
   if ($n == $pn) ... not linked ... else ... linked ...
}