Welcome Guest, Not a member yet? Register   Sign In
Search by field only when set
#1

[eluser]Andy UK[/eluser]
Hi guys,

I'm looking for a way to refine the results of a search by adding search criteria and running it again. Basically i want to do something like this but with activerecords:

http://stackoverflow.com/questions/11460...h-criteria

For example, my initial search returns a list of properties based on city, price and number of bedrooms. I would now like to filter by those results to show properties in a specific neighbourhood with 2 parking spaces.

Here is a snippet of my current code that returns the initial result

Code:
$q = $this->db->select("*")
   ->where('city_id =', $this->session->userdata('city_id'))
   ->where("{$price} >= {$priceMin}")
   ->where("{$price} <= {$priceMax}")
   ->where('beds >=', $this->session->userdata('bedsMin'))
   ->where('beds <=', $this->session->userdata('bedsMax'))
   ->where('published', '1')
   ->limit($limit, $offset);

What i want is a way to add criteria to the initial search query so that it can run for both the initial search and for subsequent searches to filter the results. One possible solution would be if i could write a query that involves some kind of wildcard (*) like here:

Code:
// Set default values if session variables are empty as is the case with the initial search

if($this->session->userdata('neighbourhood') == '')
  {
   $neighbourhood = *;
  } else {
   $neighbourhood = $this->session->userdata('neighbourhood');
  }

if($this->session->userdata('parking_spaces') == '')
  {
   $parking_spaces = '9999';
  } else {
   $parking_spaces = $this->session->userdata('parking_spaces');
  }

$q = $this->db->select("*")
   ->order_by($sort_by, $sort_order)
   ->where('city_id =', $this->session->userdata('city_id'))
   ->where("{$price} >= {$priceMin}")
   ->where("{$price} <= {$priceMax}")
   ->where('beds >=', $this->session->userdata('bedsMin'))
   ->where('beds <=', $this->session->userdata('bedsMax'))
   ->where('neighbourhood', $this->session->userdata('neighbourhood'))
   ->where('parking_spaces', $this->session->userdata('parking_spaces'))
   ->where('published', '1')
   ->limit($limit, $offset);

Whilst with the parking spaces i can simply set a very high default value, with the neighbourhood i need to select one in particular or show all of them if no filter has been set. However, as you probably already know, the above does not work. Can anyone help point me in the right direction?




Theme © iAndrew 2016 - Forum software by © MyBB