CakePHP – Search bar

This code snippet will create a search box that uses can use to search for text in the database.
The control creates a query to search certain fields of publsihed stories

View or layout:
<?php
echo $form->create('Stories', array('url' => array('action' => 'index')));
echo $form->input('search_text', array('label' => false));
echo $form->end('Search');
?>

Controler:
<?php
function admin_index( )
{
$conditions = array( 'Story.terms !=' => '0' ) ;
if( isset( $this->data['Stories']['search_text'] ) )
{
$this->set('title', ' Search "'. $this->data['Stories']['search_text'] .'"' );
$conditions = array(
'Story.published' => '1',
'or' => array(
'Story.title LIKE' => '%' . $this->data['Stories']['search_text'] . '%',
'Author.name LIKE' => '%' . $this->data['Stories']['search_text'] . '%'
));
}

$data = $this->paginate( 'Story', $conditions);
$this->set('data', $data );
}
?>

One Response to CakePHP – Search bar

  1. gfdgd says:

    this is not full code

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>