0

我看到有很多关于动态下拉列表的问题,所以我希望我的不会被掩盖。我在一个模型中有这个方法,它从表中提取数据并创建一个数组以用于构建 codeigniter 下拉列表。

 // method in the model
 public function house_dropdown()
 {
      $dbres = $this->db->get('house'); //used php5 method chaining
       //cycle through result and create array to be made a drop down.
      $dbarray = array();
      foreach($dbres->result_array() as $db)
      {
         $dbarray[$db['house_id']] = $db['house_name'];
       }
         return $dbarray;
}
这将创建数组并在控制器中使用并发送到如下视图:
 $this->data['house'] = $this->gen_model->house_dropdown();
 $this->template->set($this->data)
                       ->set_partial('maincontent','partials/maincontent/admin/register')
                       ->build('layouts/default');

// creates a from element like so echo form_dropdown($house);

但这是我在下拉元素处得到的错误:

A PHP Error was encountered
Severity: Warning
Message: Illegal offset type in isset or empty
Filename: helpers/form_helper.php
Line Number: 319
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: helpers/form_helper.php
Line Number: 329

所以我很难过!因为我不知道去哪里看,因为我知道表单助手不会有错误,而且在我的代码中我什么也看不到。任何帮助将不胜感激。提前致谢。

PS。在 foreach 之后,我在 $dbarray 上做了一个 var 转储,它表明该数组已被填充。

4

1 回答 1

4

您向函数传递了错误数量的参数,应该是

echo form_dropdown('name_of_the_dropdown', $house, 'current_selected_item');

codeigniter文档

$options = array(
     'small'  => 'Small Shirt',
     'med'    => 'Medium Shirt',
     'large'   => 'Large Shirt',
     'xlarge' => 'Extra Large Shirt',
 );

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, 'large');

会产生:

<select name="shirts">
    <option value="small">Small Shirt</option>
    <option value="med">Medium Shirt</option>
    <option value="large" selected="selected">Large Shirt</option>
    <option value="xlarge">Extra Large Shirt</option>
</select>

参考: 表单助手

于 2012-07-13T23:03:08.690 回答