Announcement

Collapse
No announcement yet.

retrieving select box..

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • retrieving select box..

    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 Code:
    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 Code:
    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!

  • #2
    using the switch function would be a lot better than using if statements.

    PHP Code:

    switch ($country) {

    case 
    AF:
      echo 
    ">AF";
    break;

    case 
    AL:
      echo 
    "AL"
    break;

    case default:
     echo 
    "You must select a country";
    break;

    Comment


    • #3
      PHP Code:
      function genOptions($passedarray,$default="") {
          while (
      $thiskey key($passedarray)) {
              
      $returnval .= "<option value=\"$thiskey\"";
              if (
      $thiskey == $default) { $returnval .= " selected"; }
              
      $returnval .= ">" $passedarray[$thiskey] . "</option>";
              
      next($passedarray);
          }
          return 
      $returnval;
      }

      $countries = array("AF" => "Afghanistan","AL" => "Albania","DZ"=>"Algeria");

      $countries asort($countries);

      $countryoptions genOptions($countries,$country); 
      This probably the best solution I can think of for you, basically it takes an array of countries and builds the select options, adding the selected attribute to the value which is passed through $country. Comments on this code are welcome, I use it in one of my own websites.

      Marc.
      Marc Hanlon
      Rushland.net

      Comment

      widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
      Working...
      X