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.
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.
Perform the following steps:
First, add the following search box to
index.ctp
inapp/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)))...