是否可以在while循环中嵌套for循环?

时间:2013-01-25 07:19:55

标签: php mysql

我想这应该是一个简单的。我很新,因为你会在我的草率代码中看到证据,但我不能让这个for循环在更大的while循环中工作。我得到一个解析错误,但我已经超过了代码HUNDREDS次,并且找不到缺少的分号或逗号。任何提示都表示赞赏。

谢谢!

        $result = mysql_query("SELECT `alias`, `max_seats` FROM `rsvp` WHERE `id` = '$id' ");
        $alias = $r['alias'];
        $seats = $r['max_seats'];

        while($r = mysql_fetch_array($result)){
            echo '<h3>Welcome, ' . $alias . '. Please complete your RSVP</h3>';
            echo '<form class="register" method="post">'; 
            echo '<input type="radio" id="responded" name="responded" value="1">Attending (confirm details in the next step)<br />';
            echo '<input type="radio" id="responded" name="responded" value="0"><em><strong>NOT</strong></em> Attending (we\'re sorry you can\'t make it!)<br /><div class="hide" id="hide1">';
            echo '<p>Please select the number of seats you\'d like to confirm (' . $seats . ' seats maximum)</p>';
            echo '<label for="seats">Seats</label>';
            echo '<select name="seats" id="seats">';
            for ($i=1; i=$seats; $i++){
                echo '<option value="' . $i . '">' . $i . '</option>';
            }   
            echo '</select> <input type="text" placeholder="How many Chicken?"></input><br />';
            echo '<input type="text" placeholder="How many Beef?"></input><br /></div>';
            echo '<input type="reset" value="Clear"><input type="submit" value="Submit"></form>';

3 个答案:

答案 0 :(得分:3)

是的,你可以使用嵌套循环! Click Me!


修复了循环

        for ($i=1; $i <= $seats; $i++){
            echo '<option value="' . $i . '">' . $i . '</option>';
        }

条件$i = $seats不是比较(它是赋值),仅当$ seat为零时才返回false,对于每个条件它将返回true,并且在每次迭代后也返回$i的值将设置为$seats

的设置

比较的正确方法可以是

$i != $seats  // not equals
$i == $seats  // equals
$i > $seats  // greater than
$i < $seats  // less than
$i >= $seats  // greater then or equal
$i <= $seats  // less then or equal

        $result = mysql_query("SELECT `alias`, `max_seats` FROM `rsvp` WHERE `id` = '$id' ");
        $alias = $r['alias'];
        $seats = $r['max_seats'];

        while($r = mysql_fetch_array($result))
        {
            echo '<h3>Welcome, ' . $alias . '. Please complete your RSVP</h3>';
            echo '<form class="register" method="post">'; 
            echo '<input type="radio" id="responded" name="responded" value="1">Attending (confirm details in the next step)<br />';
            echo '<input type="radio" id="responded" name="responded" value="0"><em><strong>NOT</strong></em> Attending (we\'re sorry you can\'t make it!)<br /><div class="hide" id="hide1">';
            echo '<p>Please select the number of seats you\'d like to confirm (' . $seats . ' seats maximum)</p>';
            echo '<label for="seats">Seats</label>';
            echo '<select name="seats" id="seats">';
            for ($i=1; $i <= $seats; $i++){
                echo '<option value="' . $i . '">' . $i . '</option>';
            }   
            echo '</select> <input type="text" placeholder="How many Chicken?"></input><br />';
            echo '<input type="text" placeholder="How many Beef?"></input><br /></div>';
            echo '<input type="reset" value="Clear"><input type="submit" value="Submit"></form>';
        }

答案 1 :(得分:1)

是..您可以在while循环中使用for循环。 在你的代码中你缺少一个$ in for循环。它应该只是这样。 你还需要在for循环中写一个条件。

for ($i=1; $i==$seats; $i++){

答案 2 :(得分:0)

这是正确的,请检查一次....................

$result = mysql_query("SELECT `alias`, `max_seats` FROM `rsvp` WHERE `id` = '$id' ");

        while($r = mysql_fetch_array($result)){
            $alias = $r['alias'];
            $seats = $r['max_seats'];
            echo '<h3>Welcome, ' . $alias . '. Please complete your RSVP</h3>';
            echo '<form class="register" method="post">'; 
            echo '<input type="radio" id="responded" name="responded" value="1">Attending (confirm details in the next step)<br />';
            echo '<input type="radio" id="responded" name="responded" value="0"><em><strong>NOT</strong></em> Attending (we\'re sorry you can\'t make it!)<br /><div class="hide" id="hide1">';
            echo '<p>Please select the number of seats you\'d like to confirm (' . $seats . ' seats maximum)</p>';
            echo '<label for="seats">Seats</label>';
            echo '<select name="seats" id="seats">';
            for ($i=1; $i=$seats; $i++){
                echo '<option value="' . $i . '">' . $i . '</option>';
            }   
            echo '</select> <input type="text" placeholder="How many Chicken?"></input><br />';
相关问题