从过滤器中选择多个数据库

时间:2015-11-18 07:40:34

标签: php mysql codeigniter activerecord

我的网站网址如下所示:

http://example.com/realestate/type-apartament/rooms-3-2/key-value1-value2-value3

所以从这里我得到一个这样的数组:

[type] => Array
    (
        [0] => apartament
    )

[rooms] => Array
    (
        [0] => 3
        [1] => 2
    )

[key] => Array
    (
        [0] => value1
        [1] => value2
        [2] => value3
    )

我从db中选择的内容应如下所示:

Select * from properties a
LEFT JOIN properties_type b
ON a.property_type_id = b.property_type_id
WHERE b.property_type = apartament
AND a.rooms = 3 AND a.rooms = 2

如何根据我拥有的键值生成此选择?我唯一的解决方案是创建一个数组助手,每个键和相应的表,外键,列......

这是我从url处理过滤器的函数:

if(!function_exists('process_filters')) {

    function process_filters($filters) {
        $data = array();
        foreach($filters as $filter):
            $filter_exploded = explode('-', $filter);
            $val = array_shift($filter_exploded);
            $data[$val] = $filter_exploded;
        endforeach;
        return $data;
    }

}

其中$ filters来自控制器方法:

public function index(...$params)

1 个答案:

答案 0 :(得分:0)

我将假设您的过滤器已在$filter变量中设置。 然后你需要调用你的过滤器模型。例如,我将假设您的模型名称为Filter_model 然后你需要从你的控制器调用model_filter,如下所示:

$this->load->model('Filter_model');
$result = $this->Filter_model->model_filter($filter); 

function model_filter($filter)
{
    $this->db->select('*');
    $this->db->from('properties');
    $this->db->join('properties_type', 'properties.property_type_id = properties_type.property_type_id', 'left');
    foreach ($filter as $key => $index) 
    {
        foreach ($index as $number => $value)
            $this->db->where($key => $value);
    }
    $data = $this->db->get()->result_array();

    return $data;
}