drlhsfqoa350437979
drlhsfqoa350437979
2014-09-17 20:03
浏览 35
已采纳

为什么在使用for循环时不会填充我的选项列表

When trying to dynamically create a select box with values using php my data will not populate. All of the research I've done shows I've done this right. Can anyone point me to the right direction please.

<select name="month">
<?php
    $months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

    for ($x=0; $x<=11; $x++) {
        echo '<option value="' . $x+1 . '">' . $months[$x] . '</option>';
    }
?>
</select>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhizhai4070
    dongzhizhai4070 2014-09-17 20:09
    已采纳

    you should add parentheses around the $x+1

    like so:

    <select name="month">
    <?php
        $months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
    
        for ($x=0; $x<=11; $x++) {
    
            echo '<option value="' . ($x+1) . '">' . $months[$x] . '</option>';
        }
    ?>
    </select>
    
    点赞 评论

相关推荐