Page 1 of 1

Drop down question

Posted: Fri Sep 02, 2005 2:40 pm
by Think Pink
Hy, I have a question regarding dropdowns.

I have a table countries ...
idCountry countryName
1 Italy
2 France
3 England

and another table people
idName personName countryCode
1 Jim 1
2 John 2
3 Michael 1

this would look like
idName personName countryName
1 Jim Italy
............................................... ..............................................................................
2 Michael Italy

I have a page profile.php where users can modify their profile.
Could you pls advice how to make a dropdown where are listed all countryes from table countries, but the selected country to be the one the user comes from.

Example
profile jim

Code: Select all

<select name="">
<option value="3">England</option>
<option value="2">France</option>
<option selected value="3">Italy</option>
</select>
profile john

Code: Select all

<select name="">
<option value="3">England</option>
<option value="2" selected>France</option>
<option value="3">Italy</option>
</select>
hope i made my self clear.
thx in advance

Posted: Fri Sep 02, 2005 2:53 pm
by feyd

Code: Select all

$query = mysql_query('SELECT * FROM countries') or die(mysql_error());
$countries = array();
while($countries[] = mysql_fetch_assoc($query));
array_pop($countries);
mysql_free_result($query);

print_r($countries);

$query = mysql_query('SELECT * FROM people') or die(mysql_error());
while($person = mysql_fetch_assoc($query))
{
  echo $person['personName'].': <select name="people['.$person['personName'].']"><option value="">Select Country</option>';
  foreach($countries as $country)
  {
    echo '<option value="'.$country['idCountry'].'"'.($country['idCountry'] == $person['countryCode'] ? ' selected="selected"' : '').'>'.$country['countryName'].'</option>';
  }
  echo '</select><hr />';
}

Drop down question

Posted: Fri Sep 02, 2005 3:09 pm
by Think Pink
thankyou.
works great.