retrieving select box..

They have: 164 posts

Joined: Nov 2001

i have a select box which display all the countries. when user register, they will need to choose they are from which country.

my problem is when retrieve out the country for display purpose. how am i suppose to do it?

this is part of the code:

<?php
echo \"<SELECT class=ft1 name=country>\";
echo \"<option>Please Select</option>\";
echo \"<option value=AF>Afghanistan</option>\";
echo \"<option value=AL>Albania</option>\";
echo \"<option value=DZ>Algeria</option>\";
?>

for normal select box, i will do something like this when retrieving..

<?php
echo \"<option \";
if (
$country == '')
  echo \"selected\";
echo \">\";

echo \"<option value=AF \";
if (
$country == 'AF')
  echo \"selected\";
echo \" >Afghanistan\";

echo \"<option value=AL \";
if (
$country == 'AL')
  echo \"selected\";
echo \" >Albania\";
echo \"</option>\";
?>

i dowan to use this way because the country list is too long. if ihave to use this method, i wil have to do if statement for each and every country.

is there any alternative ways that i can do? shorter method?? better ways?? pls help!
Confused

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

try this...

<?php
$country_list
= array(
               
// arrcy(abrv, country)
               
array(\"AF\", \"Afghanistan\"),
                array(\"AL\", \"Albania\"),
                array(\"DZ\", \"Algeria\")
                );

echo \"<select class=\\"
ft1\\" name=\\"country\\">\n\";
echo \"  <option>Please Select</option>\n\";
foreach (
$country_list as $tmp) {
   
$selected = ($country == $tmp[0]) ? 'SELECTED' : '';
    echo \"  <option value=\\"
$tmp[0]\\" $selected>$tmp[1]</option>\n\";
}
echo \"</select>\n\";
?>

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 164 posts

Joined: Nov 2001

i have another page where i display the country in a text box. what should i do then?

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

<?php
$country_list
= array(
               
// arrcy(abrv, country)
               
array(\"AF\", \"Afghanistan\"),
                array(\"AL\", \"Albania\"),
                array(\"DZ\", \"Algeria\")
                );

echo \"<input type=\\"
text\\" value=\\"\";
foreach (
$country_list as $tmp) {
    if (
$country == $tmp[0]) echo $tmp[1];
}
echo \"\\"
/>\n\";
?>

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 164 posts

Joined: Nov 2001

thanks!!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.