【问题标题】:PHP do loop doesnt exitPHP do循环不退出
【发布时间】:2013-06-11 10:16:06
【问题描述】:

我制作了一个小脚本,它应该随机重定向到 8 页之一。在选择页面之前,我会询问数据库,以缩小选择范围。 (这样每一页都是在同一个种子上挑选的)。 不幸的是,我陷入了 do-while 循环。 代码如下:

 <?php
$surveys = array(
    1 => "711275", 
    2 => "488985", 
    3 => "515385",
    4 => "467411",
    5 => "755429",
    6 => "335888",
    7 => "673921",
    8 => "532261");
$surveysCount = array(
    1 => 0, 
    2 => 0, 
    3 => 0,
    4 => 0,
    5 => 0,
    6 => 0,
    7 => 0,
    8 => 0);
$path = 'http://www.unet.univie.ac.at/~a0106191/index.php/';
$pathPostfix = '/lang-de';  

$user = 'xxxx';
$pass = 'xxxx';
$host = 'xxxx'; 

// establish database connection
$conn = mysql_connect($host, $user, $pass) or die('Cannot connect to Database - Please try later!');

mysql_select_db('a0106191', $conn) or die('Cannot connect to Database - Please try later!');

foreach ($surveys as $nr=>$survey) {
    $stmt = 'SELECT submitdate FROM lime_survey_'.$survey.' WHERE submitdate IS NOT NULL';
    //echo $stmt.'<br/>';
    if($res = mysql_query($stmt)) {
        if (mysql_num_rows($res) > 0) {
            $surveysCount[$nr] = mysql_num_rows($res);
        }
    }

    //echo $surveysCount[$nr].'<br/>';
}
$selection = array();
$counter = 0;
do {
    $offset = 0;
    for ($i = 1; $i < 9; $i++) {
        if ($surveysCount[$i] === $offset){
            $sel = $surveys[$i];
            $selection[] = $sel;
            $counter++;             
        }
    }
    $offset++;
    //echo $selection;
} while ($counter < 1);
// clean up connections
mysql_close($conn);
$whichSurveyNr = mt_rand(1, $counter);
$which = $selection[$whichSurveyNr-1];
$which = $path.$which.$pathPostfix;
  echo "wSrNr:";
    echo $whichSurveyNr;
echo "selection: ";
echo count($selection);
echo " which: ";
echo $which;
    foreach($selection as $k => $v)
        echo " sel: k: ".$k." v: ".$v;*/
header("Location: $which"); 
?>

【问题讨论】:

  • 您的计数器在条件内递增。如果该条件永远不满足,$counter 将始终小于 1。
  • if ($surveysCount[$i] === $offset){ 始终是FALSE
  • 你能分享你遇到的错误吗?
  • 在 if ($surveysCount[$i] === $offset){ 之前做 print_r($surveysCount[$i]){ 确保你有你认为你应该做的事情。

标签: php do-loops


【解决方案1】:

您目前总是在循环内重置偏移量。我不知道这是你想要的。

试试

$offset = 0;
do {

    for ($i = 1; $i < 9; $i++) {
        if ($surveysCount[$i] === $offset){
            $sel = $surveys[$i];
            $selection[] = $sel;
            $counter++;             
        }
    }
    $offset++;
    //echo $selection;
} while ($counter < 1);

【讨论】:

    【解决方案2】:

    你需要使用 == 而不是 ===,它总是会返回 false

    do {
         $offset = 0;
        for ($i = 1; $i < 9; $i++) {
            if ($surveysCount[$i] == $offset){
                $sel = $surveys[$i];
                $selection[] = $sel;
                $counter++;             
            }
        }
        $offset++;
        //echo $selection;
    } while ($counter < 1);             
    

    【讨论】:

    • 因为他需要增加计数器,而不是减少它。他正在增加它。
    • 他需要超过 0 才能退出循环,将计数器从 0 递减也无济于事。 Counter = 0, Counter -1 仍然小于
    • 为什么===总是返回false,都是整数
    • == 将追踪等于,而 === 将追踪等于
    • 是的,我理解这两者都是整数,只要它们具有相同的数字,即只要它们 == 那么它们也将 === 你的编辑将没有区别,除非一个不是一个整数
    【解决方案3】:

    您需要确保 $counter 正在递增。

    确保$surveysCount[$i] === $offset 总是发生。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多