Welcome Guest, Not a member yet? Register   Sign In
How to populate a dropdown from result_array
#7

(This post was last modified: 01-13-2015, 02:26 PM by ivantcholakov. Edit Reason: Fixing a name )

It would be better within Contacts_model a special method public function dropdown() { ... } to be created. In this special method it is not necessary all the fields to be retrieved, 'id' and 'names' are enough.

Code:
public function dropdown() {

    $result = $this->db
        ->select('id', 'names')
        ->get('departments')
        ->result_array();

    return array_columns($result, 'names', 'id');
}


And then the dropdown in the view will become:

Code:
echo form_dropdown(
            'department',
            array('' => '-- Choose --') + $departments /* as a result of $this->contacts_model->dropdown() */,
            '', // or set_value('department', $department)
            'id="department"'
        );
Reply


Messages In This Thread
RE: How to populate a dropdown from result_array - by ivantcholakov - 01-13-2015, 02:26 PM



Theme © iAndrew 2016 - Forum software by © MyBB