How I implemented message search:
Search Form Code:
<?php echo $form->create('Deal',array('action' => 'search')); echo $form->input('Deal.search'); echo $form->end('Search'); ?>
In the controller, enter the following search function:
function search() { if (!empty($this->data)) { $searchstr = $this->data['Post']['search']; $this->set('searchstring', $this->data['Post']['search']); $conditions = array( 'conditions' => array( 'or' => array( "Post.title LIKE" => "%$searchstr%", "Post.description LIKE" => "%$searchstr%" ) ) ); $this->set('posts', $this->Post->find('all', $conditions)); } }
View Code:
<?php foreach ($posts as $post): ?> <tr> <td><?php echo $post['Post']['id']; ?></td> <td> <?php echo $html->link($post['Post']['title'],'/posts/view/'.$post['Post']['id']);?> </td> <td><?php echo $post['Post']['created']; ?></td> </tr> <?php endforeach; ?>
It may not be the best / elegant, but works well for simple queries.
Ashok
source share