AikenDrum Posted October 5, 2009 Share Posted October 5, 2009 Hi there, greetings. I have a bit of working code that I need to modify, and just know that someone here can help. I simply need to add a default value to the option list - it should appear as a default in the list (i.e. the list is given alphabetically, but this has to appear at the top) The default value in question is "Germany" - (I know - never mind) $landMenu = '<select style="width: 183px" id="ZS_Land" name="ZS_Land" onchange="addCheckBox()">\n'; foreach ($records as $record){ $land =$record->getField('english'); if(empty($land)){ unset($land); continue; } $landMenu .= "<option value='$land'>$land</option>\n"; } $landMenu .= '</select><br clear ="all"/>'; } echo $landMenu Any help greatly appreciated - and when you visit Scotland, rewarded with a fish supper Sincere thanks, Aiken D Link to comment https://forums.phpfreaks.com/topic/176533-add-a-default-value-to-my-select-options/ Share on other sites More sharing options...
Bricktop Posted October 5, 2009 Share Posted October 5, 2009 Hi AikenDrum, Something like the below should work: $landMenu .= '<option value="'.$land.'" '.($land == "Germany" ? 'selected="selected"' :'').' >'.$land.'</option>'; Just inserted an ternary operator statement to check the $land variable and output "selected" if it's found. Hope this helps. Link to comment https://forums.phpfreaks.com/topic/176533-add-a-default-value-to-my-select-options/#findComment-930567 Share on other sites More sharing options...
AikenDrum Posted October 5, 2009 Author Share Posted October 5, 2009 That was just the ticket - very many thanks indeed, Kindest AikenD Link to comment https://forums.phpfreaks.com/topic/176533-add-a-default-value-to-my-select-options/#findComment-930578 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.