views:

215

answers:

5

I have a select menu that displays countries. It looks at the DB and judging by the value in the db shows the option as selected. Is there a simpler way off doing this than:

if ($country == 'AG') {echo '<option value="AG" selected="selected">Antigua</option>';} 
else {echo '<option value="AG">Antigua</option>';};

if ($country == 'AR') {echo '<option value="AR" selected="selected">Argentina</option>';} 
else {echo '<option value="AR">Argentina</option>';};

if ($country == 'AM') {echo '<option value="AM" selected="selected">Armenia</option>';} 
else {echo '<option value="AM">Armenia</option>';};

if ($country == 'AW') {echo '<option value="AW" selected="selected">Aruba</option>';} 
else {echo '<option value="AW">Aruba</option>';};

if ($country == 'AU') {echo '<option value="AU" selected="selected">Australia</option>';} 
else {echo '<option value="AU">Australia</option >';};

if ($country == 'AT') {echo '<option value="AT" selected="selected">Austria</option>';} 
else {echo '<option value="AT">Austria</option>';};

if ($country == 'AZ') {echo '<option value="AZ" selected="selected">Azerbaijan</option>';} 
else {echo '<option value="AZ">Azerbaijan</option>';};

if ($country == 'BS') {echo '<option value="BS" selected="selected">Bahamas</option>';} 
else {echo '<option value="BS">Bahamas</option>';};

if ($country == 'BH') {echo '<option value="BH" selected="selected">Bahrain</option>';} 
else {echo '<option value="BH">Bahrain</option>';};

There are a lot of countries and doing this would be madness wouldn't it????

+1  A: 

Maybe something like this will works:

$myCountry = "PL";

foreach ($countries as $country) {
  echo '<option value = "'BH'"'

  if ($country == $myCountry) {echo ' selected="selected"';}

  echo '>Bahrain</option>';
}

Where $countries is array or recordset with all countries data.

Depends on type of $countries you have to replace Bahrain data in example above with data taken from $countries.

Grzegorz Gierlik
+2  A: 

Yes it would, read the DRY principle. Iterate through your resultset of countries and check inside the loop if that country is selected, like so:

$selected_value = 'AG'; // set to stored preference or something
foreach ($countries as $code => $country) {
  echo '<option value="' . htmlspecialchars($code) . '"';
  if ($code == $selected_value) {
    echo ' selected="selected"';
  }
  echo '>' . htmlspecialchars($country) . '</option>';
} 
Peter Kruithof
A: 

Something like:

$countries = array('AG' => 'Antigua', 'AR' => 'Argentina', ...);

foreach($countries as $code => $name) {
    $selected =  ($country == $code) ? 'selected="selected"' : '';
    echo '<option value="' . $code . '"' . $selected . ' >' . $name . '</option>';
}
Felix Kling
A: 

Let $countries be the array of countries from the database. Let the specified country value be inside variable $country

foreach($countries as $value)
{
    if($country == $value['code'])
       echo '<option value="' . $value['code'] .'" selected="selected">'. $value['name'] . '</option';
    else
       echo '<option value="' . $value['code'] . '">' . $value['name'] . '</option>';
}
deostroll
A: 

Whenever you have a lot of code, that do practically the same it's a sign to use a loop or a function instead of rewriting thousands lines of code.

As Michael B. noticed on of the easies way is to use an array and a simple loop to display this list and mark correct element as selected.

$countries = array(
     'us' => 'United States of America',
     'de' => 'Germany',
     'pl' => 'Poland',
     'fr' => 'France',
     ...
);

foreach ($countires as $code => $name) {
    echo '<option value="' . $code . '" ' . ($country == $code ? 'selected="selected"' : null) . '>' . $name . '</option>';
}
Crozin