0

我想这应该很容易。我很新,正如你会在我草率的代码中看到的那样,但我无法让这个 for 循环在更大的 while 循环中工作。我收到一个解析错误,但我已经检查了数百次代码,但找不到丢失的分号或逗号。任何提示表示赞赏。

谢谢!

        $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>';
4

3 回答 3

3

是的,您可以使用嵌套循环!点击我!


固定for循环

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

条件$i = $seats不是比较(它是赋值),仅当 $seats 为零时返回 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>';
        }
于 2013-01-25T07:22:43.317 回答
1

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

for ($i=1; $i==$seats; $i++){
于 2013-01-25T07:23:45.700 回答
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 />';
于 2013-01-25T07:25:08.087 回答