简体   繁体   English

PHP While-选择html标记

[英]PHP While - SELECT html tag

I have an issue, I just cannot solve. 我有一个问题,我只是无法解决。 I have a PHP script, which loops my membership packages from my database. 我有一个PHP脚本,该脚本从数据库中循环我的会员资格包。 I have now 5 membership packages, to 3 different memberships. 我现在有5个会员套餐,到3个不同的会员资格。

I want a SELECT option. 我想要一个SELECT选项。 So users can select what package they want, to each membership. 因此,用户可以为每个成员资格选择所需的软件包。

But when I tries to do a while loop, and loop it out, it just loops out ALL 5 membership packages. 但是,当我尝试执行while循环并将其循环出去时,它只会循环出所有5个成员资格包。 I want 3 SELECTs, with the membership package details inside. 我想要3个SELECT,其中包含会员资格包详细信息。

My current code: 我当前的代码:

                                <?php
                                $p=mysql_query("SELECT * FROM membership_packages");
                                while($mp = mysql_fetch_assoc($p)):
                                    echo '      <select>
                                                <option>'.$mp['duration'].' Days</option>
                                                 </select>
                                          ';

                                endwhile;
                            ?>
<label for="c1">Selcciona la clave:</label>
        <select>
        <?php 
            $i=0;
            $var="10-";
            while ( $i<= 100) {
                echo $var." $i <option>";
                $i = $i + 1;
            }
        ?>

Either do the limit as someone else suggested or (if you want the logic only way of doing it, eg you dont know mysql) you can use and a iteration counter to stop the while loop something like: 可以按照其他人的建议进行限制,或者(如果您只想以逻辑方式,例如您不了解mysql),可以使用一个迭代计数器来停止while循环,例如:

i = 0 ;
while($mp = mysql_fetch_assoc($p) && i <=2):

echo '      <select>
<option>'.$mp['duration'].' Days</option>
</select>
  ';
 i++;

endwhile;

Edit: went to 4 not 3. 编辑:去4不3。

In addition to NoviceCoding's solution, you most definitely want to pull the opening and closing select tags out of the while loop. 除了NoviceCoding的解决方案之外,您绝对想从while循环中拉开和关闭select标签。 You only want those once but multiple option inside. 您只需要一次但多个选项。

i = 0 ;
echo '<select>';
while($mp = mysql_fetch_assoc($p) && i <3):
    echo '<option>'.$mp['duration'].' Days</option>';
    i++;
endwhile;
echo '</select>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM