Jump to content


Photo

Change selected list drop down value...


  • Please log in to reply
1 reply to this topic

#1 lidds

lidds
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 16 April 2006 - 12:32 PM

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 advance

Simon

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 16 April 2006 - 12:57 PM

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:
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>";
The countries array file looks like:
$countries = array("Afghanistan", "Albania", lots more sountries, "Zambia", "Zimbabwe");

Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users