views:

65

answers:

2

Hello,

Well, I keep improving my form generation classes and stuck in returning all the country elements in country_data array. Only first two elements is displaying on dropdown options.

Here is dropdown class:

//drop down form class
class DropDown
{
  function __construct ($form, $field_label, $field_name, $field_desc, $dropdown_data, $locale){
    $this->form = $form;
    $this->field_label = $field_label;
    $this->field_name = $field_name;
    $this->field_desc = $field_desc;
    $this->dropdown_data = $dropdown_data;
    $this->locale = $locale;
  }

  function getNotRequiredData(){
    global $notReqArry;
    return $notReqArry[$this->locale];
  }

  function getValue(){
    return $_POST[$this->field_name];
  }

  function option(){
    foreach ($this->dropdown_data as $key=>$value){
      return $options = sprintf('%s',$key,$value);
    };
  }
  function dropdown(){
    return $select_start = "field_name\">$this->field_desc".$this->option()."";
  }

  function getLabel(){
    $non_req = $this->getNotRequiredData();
    $req = in_array($this->field_name, $non_req) ? '' : '*';
    return $this->field_label ? $req . $this->field_label : '';
  }

  function __toString(){
    $id = $this->field_name;
    $label = $this->getLabel();
    $field = $this->dropdown();
    return 'field_name.'">'.$label.''.$field.'';
  }
}

And I use extra function for extra options:

function generateForm ($lang,$country_list){

  switch($lang)
  {
    case 'en-US':
      //create EN web form
      echo $countryField = new DropDown ($form, 'Country', 'form_country', '--Select Country--', $country_list, 'en-US');
    break;
    case 'fr-FR':
      //create FR web form
    break;
    case 'de-DE':
      //create DE web form
    break;
    case 'ja-JP':
      //create JA web form
    break;
    default:
      //create default web form
      print('foooo');
  };
}

And I call generateForm fun at the bottom of page.

$lang='en-US';
echo generateForm ($lang,$country_list);

At the previous question, one expert mentioned the $key and $value in foreach are not object, but I do not understand what I need to more logic on here. Yeah, I really new at PHP and just have short experience on AS. I need help.

Thanks.

+3  A: 

Your options function is trying to iterate through all of the options available but always returning just the first one. Use this instead:

function options(){
  $options = '';
  foreach ($this->dropdown_data as $key=>$value){
    $options .= sprintf('<option value="%s">%s</option>',$key,$value);
  };
  return $options;
}
Cryo
Thanks a lot Cryo, it fixed the above issue. ;-)
chris
Glad to hear it chris. If you could click on the checkbox next to my answer to mark it as correct it will let everyone else know that your question was answered (and give me a little rep bonus).
Cryo
+2  A: 

It's a surprise you have two elements in your dropdown, according to your code, you should have only one. since your function

function options(){
foreach ($this->dropdown_data as $key=>$value){
  return $options = sprintf('%s',$key,$value);
};  }

Returns only the first option it gets out of the array, and exits the cycle. Collect all the items together and return in a bunch or call the option extractor from within some outer cycle and it will work.

alemjerus