Get an array of column values ​​in codeigniter

I have a table with this structure:

ID int (11)

user_id int (11)

notification_event_id int (11)

how can I get an array containing all the values ​​of the user_id column:

EX:

Array([0]=> 1,[1]=>2,[2]=>3,[3]=>4,[4]=>5,[5]=>6); 

and not looping on the value of result_array() and moving user_ids to a new array of integers

perhaps?

+6
source share
2 answers

Each row of results is itself an array, so some loop is needed! Why do you need to do this differently?

The easiest way to do what you want:

 // Model function get_all_userid() { $query = $this->db->get('table_name'); $array = array(); foreach($query->result() as $row) { $array[] = $row['user_id']; // add each user id to the array } return $array; } // Controller function user_list() { $data = $this->your_model->get_all_userid(); // get results array from model $this->load->view('your_view', $data); // pass array to view } 

Obviously, you need to customize the names of the tables / models according to the ones you are using.

+12
source

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); 
+2
source

Source: https://habr.com/ru/post/955911/


All Articles