/path/to/webroot/properties/search.php
with the following content:<?php /** * File: /path/to/webroot/properties/search.php */ include('init.php'), // Get the list of cities $query = "SELECT id, name FROM cities"; foreach ($dbh->query($query) as $row) { $viewVars['cities'][$row['id']] = $row['name']; } // Render the view render('search'),
/path/to/webroot/properties/views/search.thtml:
<form action="advanced_search.php" method="post"> <fieldset> <legend>Advanced search</legend> <div class="input"> <label>City: </label> <select name="city_id"> <?php foreach ($viewVars['cities'] as $id => $name):?> <option value="<?php echo $id; ?>"> <?php echo $name; ?></option> <?php endforeach; ?> </select> </div> <div class="input"> <label>Type: </label> <input type="radio" name="type" value="1" checked /> Residential <input type="radio" name="type" value="2" /> Commercial </div> <div class="input"> <label>Transaction Type: </label> <input type="radio" name="transaction_type" value="0" checked /> Any <input type="radio" name="transaction_type" value="1" /> Buy <input type="radio" name="transaction_type" value="2" /> Rent <input type="radio" name="transaction_type" value="3" /> PG </div> <div class="input"> <label> </label> <input type="submit" name="submit" value="Search" /> </div> </fieldset> </form>
/path/to/webroot/properties/advanced_search.php
, with the following content:<?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'] : ''; if (!empty($type)) { // Include the api class require_once('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)); } $viewVars['results'] = $client->Query(''), } render('advanced_search'),
/path/to/webroot/properties/views/advanced_search.thtml:
<div class="results"> <?php if (isset($viewVars['results']) && empty($viewVars['results']['matches'])): ?> <div class="information">No matching properties found!!!</div> <?php endif; ?> <?php if (!empty($viewVars['results']['matches'])) { print '<div class="information"> Total '.$viewVars['results']['total_found']. ' properties found</div>'; print '<ul>'; foreach ($viewVars['results']['matches'] as $match) { print '<li><a href="view.php?id=' . $match['id'] . '">Listing #' . $match['id'] .', ' . $match['attrs']['bedrooms'] . ' Bedrooms ' . $match['attrs']['area']. 'sq feet, $' . $match['attrs']['price']. '</a></li>'; } print '</ul>'; } ?> </div>
We started off by creating a form with three search filters: City, Type, and Transaction Type. The form will be posted on advanced_search.php
, which will do the actual searching and display the results.
The form appears as seen in the following screenshot:
After that, we created advanced_search.php
and added code to perform the search using the Sphinx client API class. We used SPH_MATCH_FULLSCAN
matching mode since we only want to perform filtering rather than a full-text search on all documents.
To filter the search results by city, type
, and transaction_type
, we used the SetFitler()
method. We didn't pass any term to Query()
as we are not performing a full-text scan and only doing filtering. We added code so that SetFilter()
is only called if the respective field has been posted from the form. Lastly, we created the view to display the search results. At this point, try performing searches by selecting different cities, type, and transaction type.
We want to give a user the ability to perform searches on properties whose price ranges between the specified minimum and maximum values. Similarly, we want to give the user ability to specify a minimum number of bathrooms and bedrooms that the property should have. For searching on ranges of values, we need to use Sphinx's ranged filters. Let's move forward and add ranged filters for different attributes.