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

それとも他の方法ですか?前もって感謝します

4

3 に答える 3

1

$this->db->where_in(); を使用します。複数のフィールドに単一の値が含まれているか、またはその逆をチェックする場合

あなたの場合、

$condn_arr = array('hotel_features.extra_beds','hotel_features.satellite_tv','hotel_features.airconditioning'); 
$this->db->where(1,$condn_arr); 
于 2013-10-10T10:42:53.550 に答える
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++;
    }
}
于 2013-10-10T06:32:33.263 に答える