10
<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>

Dharman
  • 30,962
  • 25
  • 85
  • 135
Flins
  • 221
  • 2
  • 7
  • 21

7 Answers7

15

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 or htmlentities.

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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
4

You can see whats available to use by doing this in the while:

var_dump($result);
exit;

That will print the first result and its array contents. Then you can see what field you want to use to populate the option. From there, you would do something like:

foreach ($result['field'] as $field) {
   print '<option value="'.$field.'">$field</option>';
}

Of course this is a very basic example, and as others have noted you may want to clean the data before putting it into a form.

Kevin
  • 13,153
  • 11
  • 60
  • 87
  • 1
    +1 for using an array, not mysql fetching functions, inside the foreach. – Bart van Heukelom Mar 17 '10 at 09:48
  • FYI, there is no benefit to declaring `value="'.$field.'"` because it is identical to the option's text. This is actually unnecessary markup bloat since all form submissions and javascript actions will correctly pick up the text as the value. – mickmackusa Sep 13 '21 at 05:43
3

I editied the last entry to this and it works perfectly. The only hassle I have now is once the user has submitted the form the dropdown goes blank... Does anyone know a simple solution

echo '<select name="course" id="course" >';
                while( $option = mysql_fetch_array($course_results)) {
echo "<option    value=".htmlspecialchars($option['cid']).">".htmlspecialchars($option['cname'])."</option>";
}
echo "</select>";
kleopatra
  • 51,061
  • 28
  • 99
  • 211
3

All the above answers will work, but are not proper and require extra work. You should not use echo to output to the screen and you don't have to. The below example assumes you are using objects containing data, but you get the idea.

<select name="sales_person">
        <?php foreach ($sales_people as $sales_person){?>
            <option value="<?=$sales_person->first_name?> <?=$sales_person->last_name?>"><?=$sales_person->first_name?> <?=$sales_person->last_name?></option>
        <?php }?>
        </select>

The point being you don't have to echo out the html

ed209
  • 828
  • 2
  • 14
  • 30
3

$selected_value="selected_value";
echo '<select name="select">';
while($row=mysql_fetch_array($result))
{
    if($selected_value==htmlspecialchars($row['column_for_value']))
    $selected=' selected';
    else
    $selected='';
    echo '<option value="'.htmlspecialchars($row['column_for_value']).'"'.$selected.'>'
    .htmlspecialchars($row['column_for_label']).
    '</option>';
}
echo '</select>';

Some addition to Pascal MARTIN code, for auto selection of some predefined value

Imran Naqvi
  • 2,202
  • 5
  • 26
  • 53
1
echo "<select>";
while( $option = mysql_fetch_array($result)) {
  echo "<option>".htmlspecialchars($option['column'])."</option>";
}
echo "</select>";
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
lbedogni
  • 7,917
  • 8
  • 30
  • 51
0
echo '<select>';
while($row=mysql_fetch_array($result)) {
  echo '<option>'.$row['whatever_index'].'</option>';
}
echo '</select>';

Replace 'whatever_index' with the column name you are fetching.

KJ Saxena
  • 21,452
  • 24
  • 81
  • 109