Codeigniter查询where_in数组

qmb5sa22  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(153)

我有各种过滤器的网站,包括类别过滤器。用户可以选择多个类别,以及一个。在MYSQL数据库中,字段为'po_category'是一个数组(64,93,97)。所以,在codeigniter我有以下代码:

$kategorija = $this->input->post('kategorija');

$this->db->select('*'); 
$this->db->from('jobs');

    if(!empty($kategorija)) {
        $cat_array = explode(',', $kategorija);
        $count_items = count($cat_array);
        
        
        
        if($count_items == '1') {
            $this->db->where("find_in_set($kategorija, po_category)");
        } else {
            $this->db->group_start();
            $count = 0;
            foreach($cat_array as $item) {
                $count++;
                
                if($count == '1') {
                    $this->db->where_in("find_in_set($item, po_category)");
            
                } else {
                    $this->db->or_where("find_in_set($item, po_category)");
                }
            }
            $this->db->group_end();
        }
    }

只有当我在过滤器中选择类别数组中的第一个数字(64)时,它才能正常工作。我想我必须使用'where_in',但它没有正常工作。
有什么建议吗?

z6psavjg

z6psavjg1#

CodeIgniter的where_in语法如下所示:

where_in('column', $array);

为什么使用find_in_set?

ttcibm8c

ttcibm8c2#

您可以取代下列程式码:

foreach($cat_array as $item) {
  $count++;
            
  if($count == '1') {
      $this->db->where_in("find_in_set($item, po_category)");  
   } else {
      $this->db->or_where("find_in_set($item, po_category)");
   }
}

使用where_in时:

$this->db->where_in('po_category', $cat_array);

相关问题