leonardo 发表于 2009-3-24 10:37:44

Active Record中where子句的括号问题

if($cata!=""){
            if(in_array("cell",$cata)){
            $this->db->or_where("cell","1");
            }
            if(in_array("equipment",$cata)){
            $this->db->or_where("equipment","1");
            }
            if(in_array("installer",$cata)){
            $this->db->or_where("installer","1");
            }
            if(in_array("integrator",$cata)){
            $this->db->or_where("integrator","1");
            }
            if(in_array("material",$cata)){
            $this->db->or_where("material","1");
            }
            if(in_array("component",$cata)){
            $this->db->or_where("component","1");
            }
            if(in_array("panel",$cata)){
            $this->db->or_where("panel","1");
            }
            if(in_array("product",$cata)){
            $this->db->or_where("product","1");
            }
      }
      if($key!=""){
            $this->db->like($condition,$key);
      }
      
      if($continent!=""){
            $this->db->where("basic.continent",$continent);
      }
      if($region!=""){
            $this->db->where("region_en",$region);
      }
$cata是复选框的数组,代表厂家的生产类型,$key是关键字,如公司名,ID,EMAIL什么的,$condition是关键字的类型,也就是字段名,$continent是哪个州,$region是国家地区。
   比如我搜索亚洲的panel和product,那么sql语句应该是where $continent='apac' and ( $cata = panel or $cata = product),但CI没有为$cata的where子句中加括号,于是就成了where $continent='apac' and$cata = panel or $cata = product ,这样就不是搜索亚洲中的公司类型。我如何给cata类型的where子句中加上括号

leonardo 发表于 2009-3-24 10:39:51

用sql语句是能实现的,可我也不知道怎样写,才能生成我所需要的SQL

Hex 发表于 2009-3-24 11:59:43

这么复杂的 where 条件就不要用 Active Record 写了,AR 是为你提供方便的,不是给你制造麻烦的。
请直接使用 SQL 写此查询。

visvoy 发表于 2009-3-24 20:05:24

->where('continent', 'apac')->where('(cata='.$this->db->escape_str($panel).' or cata='.$this->db->escape_str($product).')')
页: [1]
查看完整版本: Active Record中where子句的括号问题