Populating a drop down list control <select> from a php array is easy, the following function would do it with a breeze. Assuming an array of IT companies:
$companies = array(
'Microsoft' => 1,
'Google' => 2,
'Apple' => 3
);
Feed it as a parameter to the php function below:
function generateSelect($name = '', $options = array()) {
$html = '<select name="'.$name.'">';
foreach ($options as $option => $value) {
$html .= '<option value='.$value.'>'.$option.'</option>';
}
$html .= '</select>';
return $html;
}
with
$html = generateSelect('company', $companies);
you will get an HTML form select control as following:
<select name="company">
<option value="1">Microsoft</option>
<option value="2">Google</option>
<option value="3">Apple</option>
</select>
With data selected from SQL database, you will first convert the SQL results rows to php array and then use it with the generateSelect() function to generate HTML select control drop down list.

{ 13 comments… read them below or add one }
I get that; however, when you use mysql to read the info in the drop-down box, how to mark as selected without hardcoding something directly into html?
You mean, via javascript?
do you know how to change drop down list value when another drop down list changed?
something like this:
i have 2 drop down list,,
the 1st one is handphone brand
the 2nd one is mobile type..
the 2nd drop down list value depends on the 1st drop down list.
both of the drop down list got the value from mysql DB.
thx
I’m not sure how I’m supposed to get the dropdown box. Do I use
$html = generateSelect(‘company’, $companies);
or
Microsoft
Google
Apple
Sorry I meant to put in:
select name=”company”>
option value=”1″>MicrosoftGoogleApple</option
/select
does one have to use a mysql db to email from a drop down box in php format.
this is a small very efficent bit of coding .. thanks.
EriK
what i mean is if the drop down box has values like yours 1-3 and the options are email addresses/names of people to email. does one need a db and if so what are the field names in the db ..name/email/value?? if you could help or ask for a clearer statement from me a newbie please ask. help is greatly needed and appreciated
Any field names in the db would do. Just make sure it all corresponds when you are inputing into or outputting from the db.
thanks but that’s Greek to me. not sure what you are suggesting? do you mean i need a db and then how do i connect to it and what fields do i add to db and how to link to db to select email addresses i haven’t a clue.
Hye..i want create a simple dropdown list from D1 until D20 using ‘ARRAY PHP’ with database but i dont know how to create that.can u explain me from begin?now im already created db.my db name is “line1″ and my form name is “chip”.i hope u can show me step by step because i dun know about programming.
hi i’m new in doing php right now, i have my first task, there is already an existing program but what i need to revise is a drop down just like the comment of sucipto in this thread, the difference is that for example when the user select registered, the next text box will be enable for the input of registration NO., if he chose as not registered then the textbox will be disabled.Thank you for your help and will be highly appreciated.
$companies = array(
‘Microsoft’ => 1,
‘Google’ => 2,
‘Apple’ => 3
);
Using your example, is it possible to have this drop down list, for example, load up with ‘Apple’ (option #3) being PRE-selected?
Useless tutorial for beginners when you don’t bother to show a “complete” preview on how it should be done.
Beginners has no way to understand what you mean by
“$html = generateSelect(‘company’, $companies);”