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 回答
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);
于 2013-10-10T06:27:02.833 回答