1

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>

3 Answers 3

5
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...)

Sign up to request clarification or add additional context in comments.

1 Comment

the option value attribute could use some quotes in this example.
1

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).

2 Comments

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
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>';
-1

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

4 Comments

Dude, that will add "selected" to every item in the list.
You're right, didn't really think about it. I updated the code.
Man, $row is an integer, how will it equal 'usa' ?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.