13

I want to use php array for HTML select list. In this case it will be the list of countries but beside of country name that is listed in drop down list I need as a value of each country short country code.

The php array that I'm currently using looks like:

$wcr=array(
'Angola',
'Antigua & Barbuda',
'Armenia', 
'Austria',
'Azerbaijan', 
 .....
 );

The PHP page that using this array:

<select name="country"><option selected value=""> --------- 
<? $p=1;asort($wcr);reset($wcr);
while (list ($p, $val) = each ($wcr)) {
echo '<option value="'.$p.'">'.$val;
} ?>
</select>

The value should be short country code (something like 'ES', 'US', 'AN', ...) instead of numbers that I have right now as a values in this form. That short country codes I will write in this same PHP array somewhere, if it's possible.

How can I do that?

2
  • 1
    It doesn't really matter. You have to validate input anyway. So, you can have anything in the select list, even just <option>Angola</option> would be enough. Upon receiving this data, you can check it against some database and get a result in any form. Commented Aug 24, 2010 at 11:08
  • 2
    Using the ISO codes gives the advantage of a lot more predictability. Even just spaces in a country name are likely to complicate the process at some point in the chain, before you start dealing with unicode characters, extra long country names, etc. Commented Aug 24, 2010 at 12:04

4 Answers 4

36

Use foreach(), it's the best function to loop through arrays

your array should look like jakenoble posted

<select name="country">
<option value="">-----------------</option>
<?php
foreach($wcr as $key => $value):
echo '<option value="'.$key.'">'.$value.'</option>'; //close your tags!!
endforeach;
?>
</select>

I also made some minor adjustements to your html code. The first option in the list will be selected by default so no need to specify it ;)

EDIT: I made some edits after reading your question again

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

Comments

5

First make an associated array of country codes like so:

$countries = array(
  'gb' => 'Great Britain',
  'us' => 'United States',
  ...);

Then do this:

$options = '';
foreach($countries as $code => $name) {
  $options .= "<option value=\"$code\">$name</option>\n";
}
$select = "<select name=\"country\">\n$options\n</select>";

6 Comments

While other answers leave a space for the template use, this one doesn't. that makes it worst one.
@Col Sharpnel Wether templates are desirable depends on other factors. For instance, I am a one man team, and such limited use of template, where html and php sit next to one another, is of no value to me. In such limited use, I doubt it adds value to teams either. Suppose you want to add a class to the select, I don't see much difference in changing it where it is html mingled with php <?php echo ... or where it is completely a php string. But then, opinions vary.
@Col Sharpnel Perhaps you have use cases in mind where it really makes a difference. If you could describe such use case it'd be good learning experience and I'd be much obliged.
Oh, I see! That is the use case story! It may not have been you, in which case I apologize. But whoever the downvote came from, it brought a heartily laughter, thank you!
I do. I didn't bother to explain because I've already heard a bulletproof argument "In my local case..". But if you ask. 1st case: Another data representation. Beside usual rich HTML we have to have a "Print version" also. CSS would not handle it all. Or we have to output the same data as XML for RSS or JSON for AJAX. 2. Multiple installations. We have our application installed on 2 hosts with different design. It's easy to upgrade our software when presentation is separated and hard as hell when it's inherited
|
4

Do you mean like this:

 $wcr=array(
 "ANG" = > 'Angola',
 "ANB" = > 'Antigua & Barbuda',
 "ARM" = > 'Armenia', 
 "AUS" = > 'Austria',
 "AZB" = > 'Azerbaijan'
  );

Then in your while loop, $p is your Short code.

Improved version of Krike's loop, if using my array of short codes as array keys:

<select name="country">
<option value="">-----------------</option>
<?php
    asort($wcr);
    reset($wcr); 
    foreach($wcr as $p => $w):
        echo '<option value="'.$p.'">'.$w.'</option>'; //close your tags!!
    endforeach;
?>
</select>

Comments

2

If you are using this sort of array as shown by jakenoble I would use foreach().

$wcr=array(
 "ANG" = > 'Angola',
 "ANB" = > 'Antigua & Barbuda',
 "ARM" = > 'Armenia', 
 "AUS" = > 'Austria',
 "AZB" = > 'Azerbaijan'
  );

So the Foreach would look something like:

foreach($wcr as $short_code => $descriptive) {
    ?>
    <option value="<?php echo $short_code; ?>"><?php echo $descriptive; ?></option>
    <?php
}

Comments

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.