如何在Codeigniter的下拉列表中显示“SELECT”?

时间:2011-10-05 17:12:52

标签: codeigniter codeigniter-2

我有一个从数据库填充的下拉列表。它工作正常但在视图文件中我希望下拉列表在所有值的最顶部显示“SELECT”。请你帮帮我吗?

先谢谢

我在控制器中有这个

 // To get the batch name
$this->load->model('dropdown_batchlist');
$data['dropdown_batchlist']= $this->dropdown_batchlist->dropdown_batchlist();

这在我的模型中 -

function dropdown_batchlist() {
$this->db->select('batchname, batchid');
$records=$this->db->get('batch');

            $data=array();

            foreach ($records->result() as $row)
                {
                    $data[$row->batchid] = $row->batchname;
                }

            return ($data);
        } 

这在我的视图文件中

<?php echo form_dropdown('batchid', $dropdown_batchlist ); ?>

1 个答案:

答案 0 :(得分:3)

您只需添加'SELECT'作为数组中的第一项:

function dropdown_batchlist() {
    $this->db->select('batchname, batchid');
    $records=$this->db->get('batch');

    $data=array();

    // add it here as the first item in the array, 
    // assuming you don't have a $row->batchid of 0 in your results.
    $data[0] = 'SELECT'; 

    foreach ($records->result() as $row)
    {
        $data[$row->batchid] = $row->batchname;
    }

    return ($data);
} 
相关问题