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 Quote Link to comment 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] Quote Link to comment 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.