假设我在CI中有一个模型,返回用户需要的内容。
$find = 'something';
$id_user = Array ( [0] => 1 [1] => 5 [2] => 20 [3] => 21 ) ;所以,我必须把它放在这里,但是出了问题.
public function find_pr_by_user ($find,$id_users) {
$this -> db -> where_in ('req_by_id'.$id_users || 'check_by_id',$id_users || 'approve_by_id',$id_users);
$this -> db -> where ("(ref_no LIKE '%$find%' || pr_title LIKE '%$find%' || req_date LIKE '%$find%')");
$query = $this -> db -> get ('pr_data') ;
return $query -> result () ;
}我将error Array转换为string转换;我拥有的输入来自$find和$id_users,可以是任何内容。我期望这样的逻辑,给出表PR_DATA中的所有数组,该表在ref_no、pr_title或req_date列上有%$find%,但在req_by_id、check_by_id或approve_by_id列中只有$id_users *(1、5、20或21)。
有人能帮忙吗?
发布于 2016-09-09 01:18:33
这个答案假设您需要在代码中的第一个where_in()中插入括号。
不幸的是,CodeIgniter并不完全支持带有活动记录的括号。因此,您必须使用where(),其中包含更复杂的SQL语法。
public function find_pr_by_user ($find,$id_users) {
$id_users_glued = implode(",",$id_users);
$this->db->where('(req_by_id IN (' . $id_users_glued . ') || check_by_id IN (' . $id_users_glued . ') || approve_by_id IN (' . $id_users_glued . '))');
$this->db->where("(ref_no LIKE '%$find%' || pr_title LIKE '%$find%' || req_date LIKE '%$find%')");
$query = $this->db->get('pr_data') ;
return $query->result () ;
}在CI的活动记录中,将如何处理语法:
优先,其中将被视为:WHERE (req_by_id IN ($id_users_glued) || check_by_id IN ($id_users_glued) || approve_by_id IN ($id_users_glued)
$id_users_glued将产生类似于1,2,3,4,5的东西
第二次,其中将被视为:AND (ref_no LIKE '%$find%' || pr_title LIKE '%$find%' || req_date LIKE '%$find%')
注意:,我还没有测试代码,因为我没有您的db结构。如果不起作用就告诉我。
https://stackoverflow.com/questions/39402127
复制相似问题