0

我尝试用变量替换递归回声。

但是我得到的结果不一样

这里的工作脚本:

if ( $currentpage <= 3 && $currentpage != "all" ) {
  $a = 3; 
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a-2) . '">' . ($a-2) . '</a>';
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a-1) . '">' . ($a-1) . '</a>';
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a) . '">' . ($a) . '</a>';  
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a+1) . '">' . ($a+1) . '</a>';
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a+2) . '">' . ($a+2) . '</a>';          
}  
if ( $currentpage > 3 && $currentpage != "all" && $currentpage < $pages-2) {
  $a = $currentpage;
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a-2) . '">' . ($a-2) . '</a>';
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a-1) . '">' . ($a-1) . '</a>';
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a) . '">' . ($a) . '</a>';  
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a+1) . '">' . ($a+1) . '</a>';
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a+2) . '">' . ($a+2) . '</a>'; 
}  
if ( $currentpage >= $pages-2 && $currentpage != "all" ) {
  $a = $pages-2; 
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a-2) . '">' . ($a-2) . '</a>';
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a-1) . '">' . ($a-1) . '</a>';
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a) . '">' . ($a) . '</a>';  
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a+1) . '">' . ($a+1) . '</a>';
  echo '<a class="page bgcolor2" href="?pagenb=' . ($a+2) . '">' . ($a+2) . '</a>';          
}

我的尝试没有成功(没有错误但结果不同):

$pagenb =  '<a class="page bgcolor2" href="?pagenb=' . ($a-2) . '">' . ($a-2) . '</a>
            <a class="page bgcolor2" href="?pagenb=' . ($a-1) . '">' . ($a-1) . '</a>
            <a class="page bgcolor2" href="?pagenb=' . ($a) . '">' . ($a) . '</a>  
            <a class="page bgcolor2" href="?pagenb=' . ($a+1) . '">' . ($a+1) . '</a>
            <a class="page bgcolor2" href="?pagenb=' . ($a+2) . '">' . ($a+2) . '</a>';

if ( $currentpage <= 3 && $currentpage != "all" ) {
  $a = 3; 
  echo $pagenb;          
}  
if ( $currentpage > 3 && $currentpage != "all" && $currentpage < $pages-2) {
  $a = $currentpage;
  echo $pagenb; 
}  
if ( $currentpage >= $pages-2 && $currentpage != "all" ) {
  $a = $pages-2; 
  echo $pagenb;          
}
4

3 回答 3

3

试着移动一下代码:

        if ( $currentpage <= 3 && $currentpage != "all" ) {
            $a = 3; 
        }  
        if ( $currentpage > 3 && $currentpage != "all" && $currentpage < $pages-2) {
            $a = $currentpage;
        }  
        if ( $currentpage >= $pages-2 && $currentpage != "all" ) {
            $a = $pages-2; 
        }

$pagenb =  '<a class="page bgcolor2" href="?pagenb=' . ($a-2) . '">' . ($a-2) . '</a>
                   <a class="page bgcolor2" href="?pagenb=' . ($a-1) . '">' . ($a-1) . '</a>
                   <a class="page bgcolor2" href="?pagenb=' . ($a) . '">' . ($a) . '</a>  
                   <a class="page bgcolor2" href="?pagenb=' . ($a+1) . '">' . ($a+1) . '</a>
                   <a class="page bgcolor2" href="?pagenb=' . ($a+2) . '">' . ($a+2) . '</a>';

            echo $pagenb;          
?>
于 2013-05-26T21:46:45.053 回答
3

PHP 本身没有惰性求值(除了 eval()'ing 它只有在那时...)

但是你可以把它写成你调用的函数$a作为函数参数:

function pagenb ($a) {
    return  '<a class="page bgcolor2" href="?pagenb=' . ($a-2) . '">' . ($a-2) . '</a>
             <a class="page bgcolor2" href="?pagenb=' . ($a-1) . '">' . ($a-1) . '</a>
             <a class="page bgcolor2" href="?pagenb=' . ($a) . '">' . ($a) . '</a>  
             <a class="page bgcolor2" href="?pagenb=' . ($a+1) . '">' . ($a+1) . '</a>
             <a class="page bgcolor2" href="?pagenb=' . ($a+2) . '">' . ($a+2) . '</a>';
}

if ( $currentpage <= 3 && $currentpage != "all" ) {
        echo pagenb(3);          
    }  
    if ( $currentpage > 3 && $currentpage != "all" && $currentpage < $pages-2) {
        echo pagenb($currentpage); 
    }  
    if ( $currentpage >= $pages-2 && $currentpage != "all" ) {
        echo pagenb($pages-2);          
    }
于 2013-05-26T21:47:47.437 回答
1

这是使用分配给变量的匿名函数的解决方案:

<?php

$pages = 10;
$currentpage = 8;
$pagination = function($value) {
    for ($a = $value, $p = -2; $p <= 2; $p++) {
        echo '<a class="page bgcolor2" href="?pagenb=' . ($a+$p) . '">' . ($a+$p) . '</a>';
    }
};

if ($currentpage != 'all') {
    if ($currentpage <= 3) {
        $pagination(3);
    } elseif ($currentpage > 3 && $currentpage < $pages-2) {
        $pagination($currentpage);
    } elseif ($currentpage >= $pages-2) {
        $pagination($pages-2);
    }
}
于 2013-05-27T00:32:56.623 回答