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.

 

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.