Web表单生成器PHP类 - foreach上的空属性

chr*_*ris 1 php forms foreach class

我目前正在构建PHP类,它可以生成Web表单以实现灵活性和本地化问题.我难以为下拉输入分配键和值; 出于某种原因,foreach似乎没有得到数组变量($ country_list).这是我的代码,我遇到了困难.

require_once('_include/country_list.php');

//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->filed_desc = $filed_desc;
    $this->dropdown_data = $dropdown_data;
    $this->locale = $locale;
  }

  function getNotRequiredData(){
    global $notReqArry; //http://stackoverflow.com/questions/1415577/accessing-variables-and-methods-outside-of-class-definitions
    return $notReqArry[$this->locale];
  }

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

  function dropdown(){
    $selecedVal = $this->getValue();
    $select_start = "<select name=\"$this->field_name\"><option value=\"\">$this->filed_desc</option>";
    foreach ($this->dropdown_data as $key=>$value){
      $selected = ($key == $selecedVal ? 'selected' : '');
      $options = sprintf('<option value="%s" %s >%s</option>',$key,$selected,$value);
      print $options;
    }
    $select_end = "</select>";
    return $select_start . $options . $select_end;
  }

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

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

}



function generateForm ($lang,$country_list){
  switch($lang)
  {
    case 'en-US':
      //create EN web form
      echo $textField = new TextField($form, 'Note', 'form_note', '2', '20', '250', 'en-US');
      //echo $textField_js = new JsTextField($textField, 'onkeyup', 'return checklength(this,contact_max_warning)', 'Characters typed:');
      echo $dropDown = 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');
  };
}


<form id="frm_verification" action="<?=$_SERVER['SCRIPT_NAME']?>" method="POST">
  <?
    $lang='en-US';
    echo generateForm ($lang,$country_list);
  ?>
<p>
  <input type="button" name="reset" value="Reset">
  <input type="submit" name="submit" value="Submit">
</p>
</form>
Run Code Online (Sandbox Code Playgroud)

而country_list.php中的数组如下:

$country_main = array (
            //Main 5 countries at first
            //Those 5 countries are direct market of my previous company. you can modify whatever you want.
            "US" => "United States",
            "UK" => "United Kingdom",
            "DE" => "Germany",
            "FR" => "France",
            "JP" => "Japan",                       
);
Run Code Online (Sandbox Code Playgroud)

它当前不起作用,我检查错误日志,它说:

"PHP致命错误:无法访问...中的空属性"

我确信这是因为我对课堂及其变形或预告陈述的误解.请帮我解决这个问题; 我真的需要帮助.(是的,我是PHP的新手)

Mar*_*arz 9

foreach ($this->dropdown_data as $key->$value){
Run Code Online (Sandbox Code Playgroud)

应该读

foreach ($this->dropdown_data as $key=>$value){
Run Code Online (Sandbox Code Playgroud)

注意从变化->=>$key=>$value.否则你的代码试图访问value对象key中的属性,这显然是无效的,因为key它不是一个对象.