Jump to content

How to add multiple values in combobox from database in PHP?


angel1987

Recommended Posts

I want to add multiple values in combobox from database. Currently i am using arrays to store values from database in a while loop.

 

But when i go for displaying them in combobox it does not work, it shows all elements in one line of combobox and it stretches in width.

 

I want to list them in combobox.

 

 

Put this in the logic of your page:

$office = //If there was a previously selected value set it here

$query = "SELECT id, name FROM offices ORDER BY name";
$result = mysql_query($query);
$officeOptions = '';
while($record = mysql_fetch_assoc($result))
{
    $selected = ($office==$record['id']) ? ' selected="selected"' : '';
    $officeOptions .= "<option value=\"{$record['id']}\">{$record['name']}</option>\n";
}

 

Then in the HTML/content you would do this

<select name="office">
<?php echo $officeOptions; ?>
</select>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.