Jump to content

dynamic dropdown


calmchess

Recommended Posts

how do i code a dropdown box with php so that i can add as many fields as i like without modifying the html?

i envision an array in ad database being brought out exploded and the resulting  array is used to populate the dropdown.

coding all the database stuff isn't necessary if you can just show me how to populate the dropdown box with an array.

Link to comment
https://forums.phpfreaks.com/topic/206110-dynamic-dropdown/
Share on other sites

Hi

 

Assuming you do not need a seperate specified value:-

 

$SomeArray = array('London','Paris','Rome');
$OutDropDown = "<select name='DropDownName'><option>".implode("</option><option>",$SomeArray)."</option></select>";

 

If you want a way to do it with a value matching an ID then not sure of a way to do it in a single line

 

$SomeArray = array(1=>'London',2=>'Paris',4=>'Rome');
$OutDropDown = "<select name='DropDownName'>";
foreach($SomeArray AS $SomeId=>$SomeName)
{
$OutDropDown .= "<option value='$SomeId'>$SomeName</option>";
}
$OutDropDown .= "</select>";

 

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/206110-dynamic-dropdown/#findComment-1078417
Share on other sites

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.