Time for action - creating the simple search form

  1. Create a script /path/to/webroot/properties/index.php with the following content (this will be our home page):
    <?php
    /**
    * File: /path/to/webroot/properties/index.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'];
    }
    // Get the query and city from form (if submitted)
    $q = !empty($_POST['q']) ? $_POST['q'] : '';
    $city = !empty($_POST['city_id']) ? $_POST['city_id'] : '';
    $viewVars['q'] = $q;
    $viewVars['city_id'] = $city;
    render('index'),
    
  2. Create the view for the form at /path/to/webroot/properties/views/index.thtml:
    <form action="index.php" method="post">
    <fieldset>
    <legend>Search</legend>
    <div class="input">
    <label>City: </label>
    <select name="city_id">
    <?php foreach ($viewVars['cities']
    as $id => $name): ?>
    <?php
    $selected = '';
    if ($id == $viewVars['city_id']) {
    $selected = ' selected';
    }
    ?>
    <option value="<?php echo $id; ?>"
    <?php echo $selected; ?>>
    <?php echo $name; ?></option>
    <?php endforeach; ?>
    </select>
    </div>
    <div class="input">
    <label>Search: </label>
    <input type="text" name="q"
    value="<?php echo $viewVars['q']; ?>" />
    </div>
    <div class="input">
    <label>&nbsp;</label>
    <input type="submit" name="submit" value="Search" />
    </div>
    </fieldset>
    </form>
    

What just happened?

We created a form with two fields: a drop-down box to select the city and a textbox to enter the keyword for search.

Search results will be filtered based on the selected city and the search keyword will be matched against the full-text description, address, and zip_code fields.

We have not added the code to perform the actual search query at this point. If you open the index.php page in a browser, you will see a search form like the following screenshot:

What just happened?

Full-text search

Now let's implement the actual search logic. The keywords entered will be matched against the full-text index and the results will be filtered based on the selected city.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset