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. I have now 5 membership packages, to 3 different memberships.
I want a SELECT option. 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. I want 3 SELECTs, with the membership package details inside.
My current code:
<?php
$p=mysql_query("SELECT * FROM membership_packages");
while($mp = mysql_fetch_assoc($p)):
开发者_Python百科 echo ' <select>
<option>'.$mp['duration'].' Days</option>
</select>
';
endwhile;
?>
Either do the limit as someone else suggested or (if you want the logic only way of doing it, e.g you dont know mysql) you can use and a iteration counter to stop the while loop something like:
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.
In addition to NoviceCoding's solution, you most definitely want to pull the opening and closing select tags out of the while loop. 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>';
<label for="c1">Selcciona la clave:</label>
<select>
<?php
$i=0;
$var="10-";
while ( $i<= 100) {
echo $var." $i <option>";
$i = $i + 1;
}
?>
精彩评论