php - 多个where条件codeigniter事件记录

标签 php mysql codeigniter

我想使用 ANDOR 进行多个 where 查询。假设我的数组是:

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

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

最佳答案

你的代码应该像

 $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);

关于php - 多个where条件codeigniter事件记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35333342/

相关文章:

php - curl php ssl连接超时

mysql - 如何在查询本身不减去一天的情况下获得我的 Mysql 查询的正确日期

php - 从 GET 请求中删除等号 "="

mysql - 计数函数 - Codeigniter 和 MySQL

jquery - 删除记录/数据后如何重新加载数据表?

php - 在 PHP/Mysql 中单击按钮更新价格表

php - 如何获取当前日期和时间 YYYY-MM-DD HH : MM? LARAVEL 4

php - 如何在php上使用ajax插入数据库?

php - MySQL SUM 直到每年的特定日期

php - 同一 codeigniter 安装中的多个应用程序共享相同的 View 布局菜单