lidds Posted April 16, 2006 Share Posted April 16, 2006 I have been searching google to try and find the answer but have not found the solution.What I want to do is change the select value in a html list drop down depending on a value from a DB query. For example I have a drop down that has a list of countries (manually entered, not from DB) on a edit user details page, so if the user has already registered as country UK, I want this to be the one that is selected.I hope I explained that OK.Thank you in advanceSimon Link to comment https://forums.phpfreaks.com/topic/7541-change-selected-list-drop-down-value/ Share on other sites More sharing options...
AndyB Posted April 16, 2006 Share Posted April 16, 2006 Your code/life will be simplest if you create an array of all the countries, and then use a simple piece of code like the below to generate your drop-down and show the selected country:[code]include("country_array.php");echo "<select size='1' name='country'>";for ($i=0;$i<count($countries);$i++ { echo "<option value='". $countries[$i]. "'"; if ($country_from_db==$countries[$i]) { echo " selected style='background-color:#6ff'"; } echo ">". $countries[$i]. "</option>\n";}echo "</select>";[/code]The countries array file looks like:[code]$countries = array("Afghanistan", "Albania", lots more sountries, "Zambia", "Zimbabwe");[/code] Link to comment https://forums.phpfreaks.com/topic/7541-change-selected-list-drop-down-value/#findComment-27457 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.