0

codeIgniter form_dropdown() 函数只能接收关联数组,但我使用 result_array() 函数有多维数组。如何在 form_dropdown() 函数上获取我的数据?

4

2 回答 2

1

假设您想要一个项目下拉列表,使用 result_array():

$query = $this->db->query("SELECT id, item_name FROM items");

$options = array();

foreach ($query->result_array() as $row){
   $options[$row['id']] = $row['item_name'];
}

echo form_dropdown('my_items_dropdown', $options, '1');
于 2013-05-26T19:48:48.060 回答
0

我用这个新功能扩展了DB_result.php这个system/database

public function dropdown_array($id_field = FALSE, $list_field = FALSE) 
{
    $result = array();

    if ($id_field === FALSE || $list_field === FALSE) return $result;

    $array = $this->result_array();

    foreach ($array as $value) {
        $result[$value[$id_field]] = $value[$list_field]; 
    }
    return $result;
}

现在您可以简单地从每个模型类中调用您的新函数来生成一个符合下拉列表的数组,如下所示:

    $customers = $this->db->get('customers');
    $result = $customers->dropdown_array('id', 'name');
    return $result;
于 2014-07-20T20:20:04.237 回答