dongyulian5801 2013-06-11 10:16
浏览 91
已采纳

PHP do循环不退出

I have made a small script, which should redirect to one of 8 pages by random. Before picking the page I ask the database, to narrow the choices. (So that every page is picked on the same seed) . Unfortunatly I got stuck in the do-while loop. Here is the code:

 <?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"); 
?>
  • 写回答

3条回答 默认 最新

  • dongyong1897 2013-06-11 10:30
    关注

    You are currently always resetting offset within the loop. I don't know this is what you intend.

    Try

    $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条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题