ENG Smith
ENG Smith

Reputation: 5

How to use "where in" method in codeigniter?

Hello Everybody i have problem with my program i need check multi number in my database but when i test it just show only one result my code :

/*in mt View*/
$data = array(


          'name' => 'search_id',
          'id' => 'search_id',
          'placeholder' => 'numbers_test',
          'autofocus' =>"autofocus",
          'rows' => '20'
          );

echo form_textarea($data,set_value('search_id'));

/* in my model */

$this->db->select('*');
$this->db->from('personal_info');
$this->db->where_in('p_id', $this->input->post('search_id'));

return $this->db->get();

i waiting your help for this problem

Upvotes: 0

Views: 3933

Answers (2)

Saqueib
Saqueib

Reputation: 3520

If you are getting input as comma separated ids like in string 1,5,4,8 etc from $this->input->post('search_id') then update your code like this

/* in my model */

$this->db->select('*');
$this->db->from('personal_info');
// Explode string into array to make where_in clause work
$this->db->where_in('p_id', explode(',', $this->input->post('search_id')));

return $this->db->get();

as official docs suggest you need to provide array of options in IN clause

Upvotes: 2

Parag Tyagi
Parag Tyagi

Reputation: 8970

You have to return the result of the query. Make changes,

/* In my model */

$this->db->select('*');
$this->db->from('personal_info');
$this->db->where_in('p_id', $this->input->post('search_id'));

$query = $this->db->get();
return $query->result_array();   // You've to return the result of the query


Also as @Saqueib said in comments, try some debugging when in doubt,

echo $this->db->last_query(); exit;

OR

echo '<pre>'; print_r($query->result_array()); exit;

Upvotes: 0

Related Questions