2013-10-10 48 views
3

我正在codeigniter中的一个项目上工作。我在多个条件的基础上进行查询时遇到问题。最简单的方法是为每个属性设置独立的条件,但是我需要一个优化的方法,因为稍后我会有超过25个属性,所以我们需要查看25个条件。 下面是示例代码优化代码化查询的方法

$this->db->select('*'); 
$this->db->from('listings'); 
$this->db->join('hotel_features','listings.id = hotel_features.listing_id'); 
$this->db->where('listings.country_code',$country_t); 
$this->db->like('listings.city',$city_t); 


    if($room_features_ids != '') 
    { 
     $room_features_array[0] = "extra_beds"; 
     $room_features_array[1] = "satellite_tv"; 
     $room_features_array[2] = "airconditioning"; 
     $room_features_array[3] = "cable_tv_service"; 
     $room_features_array[4] = "bathroom"; 
     $room_features_array[5] = "phone"; 
     $room_features_array[6] = "wifi"; 
     $room_features_array[7] = "kitchen"; 
     $room_features_array[8] = "desk"; 
     $room_features_array[9] = "refrigerator"; 

     $room_features_ids = explode("-",$room_features_ids); 

        // if $room_features_array has 0,1,2 this means first 3 features are available in hotel. 
     foreach($room_features_ids as $ids) 
     { 
      if($room_features_array[$ids] == 'extra_beds') 
      { 
       $this->db->where('hotel_features.extra_beds',1);  
      } 
      else if($room_features_array[$ids] == 'satellite_tv') 
      { 
       $this->db->where('hotel_features.satellite_tv',1); 
      } 
      //and so on.... for all properties 
     } 
    } 

现在我的问题是,是否有任何优化的方式来做到这一点? 喜欢的东西

 foreach($room_features_ids as $ids) 
     { 
      $this->db->where('hotel_features.'.$room_features_array[$ids],1); 
     } 

或任何其他方式?在此先感谢

+0

是什么1你的where子句作为参数?它是静态的还是动态的? –

+0

它表示'等于1'。 – geomagas

回答

1

有25个功能,你也在看25列。如果功能数量经常发生变化,为什么不需要一对listing_idfeature列?这样,只有存在的功能需要插入到数据库中。

WHERE查询可以

foreach($room_features_ids as $ids) 
    { 
     $this->db->where('hotel_features.feature', $room_features_array[$ids]); 
    } 

,所有规定的条件必须存在。当然,因为你从现在到hotel_features单排在listings加入多行,你应该从hotel_features汇总行:

$this->db->group_by('listings.id); 
+0

不,这是不是加入多个hotel_features行。它在一行中的列名称,extra_beds,wifi等 我让他们分开只用于过滤器列。所以我可以根据单个功能 –

1
$this->db->select('*'); 
$this->db->from('listings'); 
$this->db->join('hotel_features','listings.id = hotel_features.listing_id'); 
$this->db->where('listings.country_code',$country_t); 
$this->db->like('listings.city',$city_t); 
if($room_features_ids != '') 
{ 
    $room_features_array[0] = "extra_beds"; 
    $room_features_array[1] = "satellite_tv"; 
    $room_features_array[2] = "airconditioning"; 
    $room_features_array[3] = "cable_tv_service"; 
    $room_features_array[4] = "bathroom"; 
    $room_features_array[5] = "phone"; 
    $room_features_array[6] = "wifi"; 
    $room_features_array[7] = "kitchen"; 
    $room_features_array[8] = "desk"; 
    $room_features_array[9] = "refrigerator"; 

    $ids = explode("-",$room_features_ids); 

    $counter = 0; 
    foreach($room_features_array as $key => $value){ 

     if($value == $room_features_array[$ids[$counter]]){ 
      $this->db->where("hotel_features.$value",1);  
     } 
     $counter++; 
    } 
} 
+0

来过滤什么是计数器? –

+0

增加ids索引 –

+0

几乎与我用一个额外的if循环一样:P –

1

使用$这个 - > DB-> where_in();检查多个字段包含单个值或反之亦然

在你的情况,

$condn_arr = array('hotel_features.extra_beds','hotel_features.satellite_tv','hotel_features.airconditioning'); 
$this->db->where(1,$condn_arr);