Book Image

CakePHP 2 Application Cookbook

By : James Watts, Cake Software Foundation, Inc.
Book Image

CakePHP 2 Application Cookbook

By: James Watts, Cake Software Foundation, Inc.

Overview of this book

Table of Contents (20 chapters)
CakePHP 2 Application Cookbook
Credits
Foreword
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Basic search and filter


Displaying paginated records can be great to allow your users to easily navigate through large sets of data. However, it is even better when you can give them the option to search directly.

In this recipe, we're going to add a new search box for our packages. We'll use a LIKE condition in SQL to filter the resulting packages. This will do the trick for small datasets as a starting point.

Getting ready

For our recipe, we'll assume that you still have the packages table, Package model, and PackagesController, with the paginated index() action with the index.ctp view created from our previous recipe.

How to do it...

Perform the following steps:

  1. First, add the following search box to index.ctp in app/View/Packages/ right below the <h2> element:

    <?php
      echo $this->Form->create('Package', array('action' => 'search'));
      if (!isset($searchQuery)) {
        $searchQuery = '';
      }
      echo $this->Form->input('searchQuery', array('value' => h($searchQuery)))...