Using PHP to populate a <select></select> dropdown?
dropdown, html-select, php
Solution
What about something like this :
echo '<select name="select">';
while($row=mysql_fetch_array($result))
{
echo '<option value="' . htmlspecialchars($row['column_for_value']) . '">'
. htmlspecialchars($row['column_for_label'])
. '</option>';
}
echo '</select>';
Of course, up to you to decide which items from `$row` should be used for the value and the text of each `<option>`
Just make sure you are escaping the data that comes from your DB -- especially if it can contain HTML ; as you are outputting HTML, this can be done with [`htmlspecialchars`][1] or [`htmlentities`][2].
Note that those might take a couple of additionnal parameters that I didn't use in my example -- setting those can be useful, depending on the charset you're using.
Problem
``` <select name="select"> </select> ``` I want to populate the above tag with values from database. I have written PHP code up to this. ``` while($row=mysql_fetch_array($result)) { } ``` $row is fetching correct values. How to add it to the `<select>`