LemonStand Wiki

Creating the Search page

The shop:search action allows you to create a product search page. The action loads a search query from the page URL and creates the $products PHP variable which contains a list of found products. Also the action creates the $query variable which contains the search query string.

Creating the search form

The search page requires a search form. This is a good idea to place the search form into a partial, because you may want to display the search form on all pages of your store. Below is an example of a simple search form partial. In our Demo store this partial has name 'shop:search_form', but you can assign it any name.

<form method="get" action="/search">
    <input name="query" type="text" value="<?= isset($query) ? h($query) : null ?>"/>
    <input type="submit" value="Find Products"/>
    <input type="hidden" name="records" value="6"/>
</form>

Please note that the form uses the GET method for sending the search request to the server. This is necessary, because the shop:search action reads the search query from the URL. The ACTION attribute of the search form should point to your search page. In our Demo store the Search page has the '/search' URL assigned. 

The INPUT element with the query name is required. This is field for entering a search query. The hidden input named records is optional. You can use it for specifying how many records you want to see on a single page of the search result page.

After creating the partial, you can display it on other pages, using the simple render_partial call:

<? $this->render_partial('shop:search_form') ?>

You can place this call into your Templates, then you will not need to render the search form on each page.

Creating the search page

Start with creating a new page and assign it some URL, for example /search. Select the shop:search action on the Actions tab. Below is an example of a simple Search page. The example assumes that you followed the Displaying a list of products tutorial and that you have partials for displaying the product list (shop:product_list in our example) and for displaying the pagination markup.

<h2>Product search</h2>

<? $this->render_partial('shop:search_form') ?>

<? if (strlen($query)): ?>
    <p>Products found: <?= $pagination->getRowCount() ?></p>

    <? $this->render_partial('shop:product_list', array('products'=>$products, 'paginate'=>false)) ?>

    <? $this->render_partial('pagination', array(
      'pagination'=>$pagination, 
      'base_url'=>'/search', 
      'suffix'=>'?query='.urlencode($query).'&amp;records='.urlencode($records))) ?>
<? endif ?>

Please note that we used the suffix parameter of the pagination partial for passing the query string and the number of records on a single page to other pages through the URL.

Next: Creating the new products RSS channel
Previous: Change Password page
Return to Building your online store

Comments

No comments posted so far.

Add your comment

Loading form...