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

时间:2015-03-03 06:56:52

标签: codeigniter

我正在尝试以下代码:

//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');

结果我看到'其中'使用market_product.product_id,它还有一个"和"。 我需要的是(' OR'对于市场和一个' AND'对于product_id。) 但无论我尝试了什么都没有工作..我也在stackoverflow中查看了其他类似的问题,但没有一个包含“foreach'事情或如何解决它。

任何帮助将不胜感激。我知道版本3.0会通过分组来解决它,但尚未正式发布。

1 个答案:

答案 0 :(得分: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');

希望这对你有用