black_belt
black_belt

Reputation: 6799

How to display "SELECT" in dropdown list in Codeigniter?

I a have a dropdown list which is being populated from database. It is working fine but in the view file I want the dropdown list to show "SELECT" on the very top of all values. Would you please kindly help me with this?

Thanks in Advance

I have this in my controller

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

this in my model-

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);
        } 

And this in my view file

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

Upvotes: 1

Views: 6177

Answers (1)

swatkins
swatkins

Reputation: 13630

You just have to add the 'SELECT' as the first item in the array:

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);
} 

Upvotes: 3

Related Questions