2013-10-10 81 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);