多个where条件codeigniter活动记录

时间:2016-02-11 07:37:36

标签: php mysql codeigniter

我希望使用ANDOR进行多重查询。假设我的数组是:

array (size=8) 
      0 => array (size=2) 
              'timeline.type' => string 'videos' (length=6)                
              'timeline.sourceId' => string '7' (length=1) 
      1 => array (size=2) 
              'timeline.type' => string 'loadshedding' (length=12) 
              'timeline.sourceId' => string '5' (length=1) 
      2 => array (size=2) 
              'timeline.type' => string 'news' (length=4) 
              'timeline.sourceId' => string '3' (length=1)
      3 => array (size=2) 
              'timeline.type' => string 'news' (length=4) 
              'timeline.sourceId' => string '5' (length=1) 

上面的数组可能是动态的。我想进行如下活动记录查询:

 (timeline.type = 'videos' AND timeline.sourceId = 7) OR (timeline.type = 'loadshedding' AND timeline.sourceId = 5) OR (timeline.type = 'news' AND timeline.sourceId = 3) // and so on.

我试过了:

 $this->db->select('timeline.id as postid, timeline.*, sources.*, subscription.*')->from('timeline');
 $this->db->join('sources', 'sources.type = timeline.type and timeline.sourceId = sources.id');
 $this->db->join('subscription', 'subscription.type = timeline.type and timeline.sourceId = subscription.sourceId');
 foreach($sources as $source){   //$sources is an array like given above
        $this->db->or_where($source);
 }
 $this->db->order_by("timeline.id", "DESC");
 $this->db->limit(15);

但是,当我回显$this->db->last_query();以查看查询时。它仅返回SELECT * FROM timeline。可能是什么问题。谢谢。

1 个答案:

答案 0 :(得分:2)

您的代码应该像

 $sources = array (
                    0 => array ( 
                          'timeline.type' =>  'videos',             
                          'timeline.sourceId' =>  '7' ), 
                    1 => array ( 
                          'timeline.type' =>  'loadshedding',
                          'timeline.sourceId' =>  '5' ) ,
                    2 => array (
                          'timeline.type' =>  'news', 
                          'timeline.sourceId' =>  '3' ),
                    3 => array ( 
                          'timeline.type' =>  'news',
                          'timeline.sourceId' =>  '5') 
                );
end($sources);         // move the internal pointer to the end of the array
$lastkey = key($sources);  
$pr = "";
foreach($sources as $key=>$source)
{   
    if($key != $lastkey)
    {
        $pr.="( `timeline.type` = '". $source["timeline.type"] ."' and `timeline.sourceId` = " . (int) $source["timeline.sourceId"]  . ") OR ";
    }
    else
    {
        $pr.="( `timeline.type` = '". $source["timeline.type"] ."' and `timeline.sourceId` = " . (int) $source["timeline.sourceId"]  . ")";
    }   
}
$this->db->select('timeline.id as postid, timeline.*, sources.*, subscription.*')->from('timeline');
$this->db->join('sources', 'sources.type = timeline.type and timeline.sourceId = sources.id');
$this->db->join('subscription', 'subscription.type = timeline.type and timeline.sourceId = subscription.sourceId');
$this->db->where($pr);
$this->db->order_by("timeline.id", "DESC");
$this->db->limit(15);