如何正确组合 where 和 or_where (使用 foreach 循环)

How to correctly combine where and or_where (with foreach loop)

我正在尝试以下代码:

//NEEDS TO BE 'OR's
    foreach ($market_ids as $market_id) {
             $this->CI->db->or_where('market_id',$market_id,FALSE);
             }
//NEEDS TO BE 'AND'
    $this->CI->db->where('market_product.product_id',$product_id,FALSE);
    $this->CI->db->from('market_product');
    $this->CI->db->join('product', 'market_product.product_id = product.product_id');

结果我看到 'where' 和 market_product.product_id 它还有一个 "and"。 我需要的是('OR's for the markets and one 'AND' for the product_id.) 但是无论我尝试什么都没有用..我还在Whosebug中查看了其他类似的问题但是none其中包含'foreach'的东西或如何解决它。

如有任何帮助,我们将不胜感激。我知道3.0版本会通过分组来解决,但还没有正式发布

解决您问题的方法

//NEEDS TO BE 'OR's
    $where_or = array();
    foreach ($market_ids as $market_id) {
            // $this->CI->db->or_where('market_id',$market_id,FALSE);
         $where_or[] = "market_id = $market_id";
    }
  $this->CI->db->where("(".implode(' OR ',$where_or).")");
//NEEDS TO BE 'AND'
    $this->CI->db->where('market_product.product_id',$product_id,FALSE);
    $this->CI->db->from('market_product');
    $this->CI->db->join('product', 'market_product.product_id = product.product_id');

希望这对你有用