views:

596

answers:

3

Below is my code I am trying to get to work but I really have very little knowledge of array and foreach. So it doesn;t work correctly

I need to show a dropdown select form to the browser with the contents of the array I also need to have the item selected if it is == to $mycountry Lastly I would like to show the USA and UK at the top of my list

Can anyone tell me how I can do al this

<?PHP
$countries = array(
"217" => "Turkenistan",
"218" => "Turks and Caicos Islands",
"219" => "Tuvalu",
"220" => "Uganda",
"221" => "Ukraine",
"222" => "United Arab Emirates",
"223" => "United Kingdom (Great Britain)",
"224" => "United States");


$mycountry = 224;
?>

<select name="country" style="width:180px;" onChange="do_get_rest_popup(this.value)" /> 
<?php
$countryCounter = 1;
$amtOfCountries = count($countries);
//foreach ($country as $id => $c) {
for( $_top=0; $_top < $amtOfCountries; $_top++ ){ 
    if ($countryCounter == $amtOfCountries) { 
     echo "<option value=\"$countries[0]\" SELECTED>$countries[1]</option>";
    }  else {
     echo "<option value=\"$countries[0]\">$countries[1]</option>";
     $countryCounter++;
    }
}
?>
</select>
A: 

You should put US and UK at the top of your array and then use something like:

foreach($countries as $row => $value) {
    echo "<option value=\"$row\"" + ($row == 'usa' ? 'SELECTED') + ">$value</option>";
}

and you should use selected="selected" instead of SELECTED

Dennis
Dude, that will add "selected" to *every* item in the list.
Christian
You're right, didn't really think about it.I updated the code.
Dennis
Man, $row is an integer, how will it equal 'usa' ?
Christian
It depends on the situation, I just took an example.$arr = new array('usa'=>'united states','uk'=>'United Kingdom');usually associated arrays are used when you want to print countries with their country code.
Dennis
+1  A: 

I'm going to guess you are looking for:

foreach($countries as $id => $country) {
    echo '<option value="$id"' . ($mycountry==$id?'selected="selected"':'') . '>' . $country . '</option>';
}

As for making sure that the U.S. and U.K. are on top, make sure that those 2 are on top of your array (that would be the very easiest).

Evert
thanks I like the shorter code but there is a snytax error and I don't really know how to do if/else in the shorthand method very well
jasondavis
don't you need an else (:) ?
Tom Haigh
thank you tom, missed that..
Evert
can you help me get this line working, I can't get rid of syntax errors echo '<option value="' .$sid. '"' . ($test==$sid?'selected="selected"':'')' . '>' . $statename . '</option>';
jasondavis
fixed another typo. srry :S
Evert
+5  A: 
foreach ($countries as $key => $country) {
    $selected = ""
    if ($key == $mycountry) $selected = ' selected="selected" ';
    print '<option value=' . $key . $selected . '>' . $country . '</option>';
}

Basically, for every element within the array, you are breaking it into its key and its value (ie $countreis[key] = value). Once you get your head around arrays (and they can be very confusing) it will make coding a million times easier.

(For some reason the syntax highlighting / formatting is not working in my code...)

Christian
the `option value` attribute could use some quotes in this example.
Thorarin
Woops, you're right. Thanks!
Christian