Professional CodeIgniter

(singke) #1

Chapter 4: Creating the Main Web Site


101


Leaving out the colors and sizes is intentional at this point. There wasn ’ t much discussion about these
database fields during discovery, and you suspect that Claudia will fill you in once she reviews your
work so far. That ’ s OK, because in Agile the idea is to iterate.

Displaying Search Results


Writing a search engine in CodeIgniter, and especially for this project, is pretty straightforward. As you ’ ll
see, it ’ s all about judiciously using the built - in wildcard - matching methods in your model to pull it off.

Without further ado, here ’ s a search() function you can add to the MProducts model. You pass it a
single argument, a search term, which is then used to match across the name, shortdesc, and longdesc
fields of the products table. For now, this search function returns a maximum of 50 rows in the result set:

function search($term){
$data = array();
$this- > db- > select(‘id,name,shortdesc,thumbnail’);
$this- > db- > like(‘name’,$term);
$this- > db- > orlike(‘shortdesc’,$term);
$this- > db- > orlike(‘longdesc’,$term);
$this- > db- > orderby(‘name’,’asc’);

Figure 4 - 11
Free download pdf