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 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?