简体   繁体   中英

Select from multiple database by filters

My site url is looking like this:

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

So from here I get a array like this:

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

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

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

My select from db should look like this:

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

How can I generate this select based on the key values that I have? My only solution in mind is to create a array helper with each of keys and the corresponding table, foreign key, column ...

This is my function where I process the filters from 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;
    }

}

where $filters come from the controller method:

public function index(...$params)

I will asume your filter already set in $filter variable. then you need to call your filter model. for example i will asume your model name is Filter_model then you need to call model_filter from your controller like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM