Where_in in codeigniter

别等时光非礼了梦想. 提交于 2020-03-03 04:26:56

问题


I'm using Codeigniter 3 to build a website. But now I have an issue with get data from database.

this is my Model:

public function Get($arr_condition = null)
{
     if(!is_null($arr_condition))
     {
        $this->db->where($arr_condition);
     }
     $query = $this->db->get('cus');
     if($query->num_rows()>0)
     {
        return $query->result_array();
     }
     return false;
}

In Controller, I use an array ($arr_condition) to get list conditional:

$arr_condition['Cus_team'] =  1;
$arr_condition['Cus_user != '] =  2;
$arr_condition['Cus_phone LIKE'] = 09900000;
$arr_condition['Cus_role >'] = 1;

$result = $this->M_cus_dal->Get($arr_condition);

My code working perfectly until I need a condition with Where_in, I tried:

$arr_condtion['Cus_id IN']= array('1,2,3');

But, It not work.

Please give me an ideal to resolve this issue.


回答1:


Hope this will help you :

You can do something like this :

In controller pass your where_in condition with another parameter like this

$where_in = array('1,2,3');
$result = $this->M_cus_dal->Get($arr_condition,$where_in);

In model method get $where_in as second parameter as add it to where_in clause like this :

public function Get($arr_condition = null,$where_in = NULL)
{
     if(!is_null($arr_condition))
     {
        $this->db->where($arr_condition);
     }
     if(! empty($where_in))
     {
       $this->db->where_in('Cus_id',$where_in);
     }
     $query = $this->db->get('cus');
     if($query->num_rows()>0)
     {
        return $query->result_array();
     }
     return false;
}



回答2:


If you use where_in then your ids should be in array

like this:

$where_in[] = 1;
$where_in[] = 2;
$where_in[] = 3;

OR

$where_in = array('1','2','3');


$this->db->where_in('Cus_id', $where_in);


来源:https://stackoverflow.com/questions/50861360/where-in-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!