Drop down question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

Drop down question

Post 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
Last edited by Think Pink on Fri Sep 02, 2005 2:53 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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 />';
}
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

Drop down question

Post by Think Pink »

thankyou.
works great.
Post Reply