Time for action - creating the search form

  1. Create a script /path/to/webroot/feeds/search.php with the following content:
    <?php
    /**
    * File: /path/to/webroot/feeds/search.php
    */
    include('init.php'),
    // Get all the categories and their ids
    // This will be used to build the categories filter drop down
    $query = "SELECT id, name FROM categories ORDER BY name";
    foreach ($dbh->query($query) as $row) {
    $viewVars['cat_list'][$row['id']] = $row['name'];
    }
    // Render the page
    render('search'),
    
  2. Create the view for the search page at /path/to/webroot/feeds/views/search.thtml:
    <!-- File: /path/to/webroot/feeds/views/search.thtml -->
    <form action="search.php" method="post">
    <fieldset>
    <legend>Search Feeds</legend>
    <div class="input">
    <label>Search for:</label>
    <input type="text" name="q" value="" />
    </div>
    <div class="input">
    <label>Author:</label>
    <input type="text" name="author" value="" />
    </div>
    <div class="input">
    feed search applicationfeed search applicationsearch form, creating<label>Category:</label>
    <select multiple="true" size="10" name="categories[]">
    <?php foreach ($viewVars['cat_list'] as $id => $name): ?>
    <option value="<?php echo $id; ?>">
    <?php echo $name; ?></option>
    <?php endforeach; ?>
    </select>
    </div>
    <div class="input">
    <label>&nbsp;</label>
    <input type="submit" value="Search" name="search" />
    </div>
    </fieldset>
    </form>
    
  3. Open search.php in a browser.
    Time for action - creating the search form

What just happened?

We created a basic form to specify the search phrase, author name, and select multiple categories. We built the categories select box using the category labels we have in our database table.

Search will be performed against different fields as given next:

  • Search for: Required. Full-text search against and description of feed items.
  • Author: Optional. Full-text search against author name in addition to the above.
  • Category: Optional. Filter results by the selected categories in addition to the above.

Perform the search query

The form we created does nothing but render different fields. Let's give it some life by adding the code that performs a search against the Sphinx index and displays the results. We will use a PHP implementation of the Sphinx Client API to perform search queries.

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

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