I did a research and found this:
Note: this is a βmagicβ solution for this, for example: using the custom codeigniter function, I think, does not exist in the real version of the framework. Thus, you need to create a function in the model or in the user assistant.
Link: Select dropdown list from database
Using your model
// Controller $data['city_list'] = $this->City_model->get_dropdown_list(); $this->load->view('my_view_file', $data); Model: // Model (or create a helper -- see below) function get_dropdown_list() { $this->db->from('city'); $this->db->order_by('name'); $result = $this->db->get(); $return = array(); if($result->num_rows() > 0) { foreach($result->result_array() as $row) { $return[$row['id']] = $row['name']; } } return $return; } // View <?php echo form_dropdown('city_id', $city_list, set_value('city_id', $city_id));
Using Assistant
if ( ! function_exists('drop_down')) { function drop_down($name, $match, $data) { $form = '<select name="'.$name.'"> ' ."\n"; foreach($data as $key => $value) { $selected = ($match == $key) ? 'selected="selected"' : NULL ; $form .= '<option value="'. $key .'" '. $selected .'>'.$value.'' . "\n"; } $form .= '</select>' . "\n"; return $form; } } In the view echo drop_down('mylist', 3, $data);
source share