dqnhfbc3738 2014-03-24 15:04
浏览 40
已采纳

Break While和Foreach循环

I have the following code below, my question is, I want to break out of the while loop if the user enters "n" and also continue with the next foreach interation. Please see my comment within.

foreach($nodeid as $nid){

    $query= $dbh->query("SELECT * FROM Nodes WHERE Node_ID = '$nid' AND New = 1")or die(mysql_error());

    if($query->rowCount() > 0)
    { 
        while (1) {
            fputs(STDOUT, "
"."***New Node*** : $nid 
 Do you want to add ? [y,n]: ");
            $response = strtolower(trim(fgets(STDIN)));
            if( $response == 'y' ) {
                #do something then break and go to next foreach.
            } elseif( $response == 'n' ) {
                #go to next foreach $nid
            } else {
                echo "
", "I don't understand your option, let's try this again", "
";
                continue;
            }
        }
    }
}
  • 写回答

2条回答 默认 最新

  • dsfb20227 2014-03-24 15:09
    关注

    You can:

    continue 2;
    

    in this case. This will continue with the next run of the outer foreach loop.

    However, as @Tularis told in comments, a simple break would have the same effect here because no code is following the while loop. (and might be easier to read)


    About the query. It looks like it would be better here to issue only one database query for all ids, using the IN statement:

    $ids = impldode(',', $nodeids); // assuming the ids are integers
    $query= $dbh->query("SELECT * FROM Nodes WHERE Node_ID IN $ids AND New = 1")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址