angel1987 Posted June 16, 2010 Share Posted June 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204908-how-to-add-multiple-values-in-combobox-from-database-in-php/ Share on other sites More sharing options...
Alex Posted June 16, 2010 Share Posted June 16, 2010 Post the code that you have so far. Quote Link to comment https://forums.phpfreaks.com/topic/204908-how-to-add-multiple-values-in-combobox-from-database-in-php/#findComment-1072749 Share on other sites More sharing options...
Psycho Posted June 16, 2010 Share Posted June 16, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/204908-how-to-add-multiple-values-in-combobox-from-database-in-php/#findComment-1072790 Share on other sites More sharing options...
angel1987 Posted June 18, 2010 Author Share Posted June 18, 2010 Thanks very much, the above code worked for what i was looking for. Quote Link to comment https://forums.phpfreaks.com/topic/204908-how-to-add-multiple-values-in-combobox-from-database-in-php/#findComment-1073773 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.