/path/to/webroot/properties/views/search.thtml
as highlighted in the following code:<!-- File: /path/to/webroot/properties/views/search.thtml -->
<input type="radio"
name="transaction_type" value="2" /> Rent
<input type="radio"
name="transaction_type" value="3" /> PG
</div>
<div class="input">
<label>Budget ($): </label>
<input type="text" name="min_price" size="5" /> to
<input type="text" name="max_price" size="5" />
</div>
<div class="input">
<label>Min Bedrooms: </label>
<input type="text" name="bedrooms" size="1" />
</div>
<div class="input">
<label>Min Bathrooms: </label>
<input type="text" name="bathrooms" size="1" />
</div>
<div class="input">
<label>Min Area: </label>
<input type="text" name="area" size="4" />
</div>
<div class="input">
<label>Max Age: </label>
<select name="built_year">
<option value="0">Under Construction</option>
<option value="<?php echo date('Y'), ?>">
Current Year</option>
<?php
for ($i = 1; $i <= 200; $i++) {
?>
<option value="<?php echo date('Y') - $i; ?>">
<?php echo $i; ?> Years</option>
<?php
}
?>
</select>
</div>
<div class="input">
<label> </label>
<input type="submit" name="submit" value="Search" />
</div>
</fieldset>
</form>
/path/to/webroot/properties/advanced_search.php
and add code to set ranged filters (as highlighted):<?php /** * File: /path/to/webroot/properties/advanced_search.php */ include('init.php'), $city_id = !empty($_POST['city_id']) ? $_POST['city_id'] : ''; $type = !empty($_POST['type']) ? $_POST['type'] : ''; $transaction_type = !empty($_POST['transaction_type']) ? $_POST['transaction_type'] : ''; $min_price = !empty($_POST['min_price']) ? $_POST['min_price'] : 0; $max_price = !empty($_POST['max_price']) ? $_POST['max_price'] : 0; $bedrooms = !empty($_POST['bedrooms']) ? $_POST['bedrooms'] : ''; $bathrooms = !empty($_POST['bathrooms']) ? $_POST['bathrooms'] : ''; $area = !empty($_POST['area']) ? $_POST['area'] : ''; $built_year = !empty($_POST['built_year']) ? $_POST['built_year'] : 0; if (!empty($type)) { // Include the api class require('sphinxapi.php'), $client = new SphinxClient(); // Set search options $client->SetServer('localhost', 9312); $client->SetConnectTimeout(1); $client->SetArrayResult(true); // Set the mode to SPH_MATCH_FULLSCAN // We won't do any full-text search but just filtering $client->SetMatchMode(SPH_MATCH_FULLSCAN); // Set the type filter $client->SetFilter('type', array($type)); // If we have city filter if (!empty($city_id)) { $client->SetFilter('city_id', array($city_id)); } // If we have transaction type filter if (!empty($transaction_type)) { $client->SetFilter('transaction_type', array($transaction_type)); } // If we have both min and max price for the budget if (!empty($min_price) && !empty($max_price)) { $client->SetFilterRange('price', (int)$min_price, (int)$max_price); } // We will assume that max bedrooms can be 10000 if (!empty($bedrooms)) { $client->SetFilterRange('bedrooms', $bedrooms, 10000); } if (!empty($bathrooms)) { $client->SetFilterRange('bathrooms', $bedrooms, 10000); } // We will assume that max are can be 99999999.00 if (!empty($area)) { $client->SetFilterFloatRange('area', (float)$area, 99999999.00); } // If we have built year then set the range if (!empty($built_year)) { // Range will be from selected year to current year $client->SetFilterRange('built_year', $built_year, date('Y')); } else { // If Under Construction is selected //then year attr should be 0 $client->SetFilter('built_year', array(0)); } $viewVars['results'] = $client->Query(''), } render('advanced_search'),
We modified the search form and added search (filter) fields for; budget, minimum number of bedrooms and bathrooms, minimum area of the property, and maximum age of the property.
We then modified the advanced_search.php
script and added code to collect the form data and apply the respective filters.
Let's try to understand each filter:
SetFilterRange()
method. That method takes the attribute name as the first argument, and the next two arguments as minimum and maximum values for the range boundary. SetFilterRange()
method and passed the value from the form as minimum and kept 10,000 as maximum. Thus we will get only those properties that have at least those many bedrooms or bathrooms. Here we have assumed that maximum number of bedrooms or bathrooms that any property can have is 10,000 (you can adjust this as per your needs). SetFilterFloatRange()
method in this case. This method works similar to SetFilterRange()
, with the only difference being that the former should be used for float values and the latter for integer values (attributes). built_year
attribute in the index that holds the year in which the property was built. That attribute holds the value of 0 if the property is under construction. We used conditional logic and applied the correct method to either filter on ranged values or filter for specific value.The Advanced search form (with all the ranged filters) now looks like the following screenshot:
We added filters for most of the attributes in the previous exercise. The only one remaining was amenities
, which is a multi-valued attribute in our index.
Go ahead and add a multi select drop-down box in the search form and add related code to filter the search results by amenities. The final search form should appear as seen in the next screenshot: