1
votes

I have a form with a search input:

echo $this->Form->create('Job', array('url'=>array('controller'=>'jobs', 'action'=>'search'), 'action'=>'search', 'inputDefaults' => array( 'label' => false, 'div'=>false))); 
echo '<br/>'.$this->Form->input('search', array( 'label' => false, 'div'=>false));
echo $this->Form->submit('Search', array('div' => false));

in my controller I have the paginate array like so:

public $paginate = array(
    'Job' => array(
        'fields' => array('Job.id', 'Job.name', 'Job.description', 'Job.tag_words'),
        'limit' => 5,
        'order' => array(
            'Job.name' => 'asc'
        )),
);

and then my search function is:

public function search () {
if ($this->request->is('post')) {
    $conditions = array();
    $search_terms = explode(' ', $this->request->data['Mix']['search']);
    foreach($search_terms as $search_term){
        $conditions[] = array('Mix.name Like' =>'%'.$search_term.'%');
        $conditions[] = array('Mix.description Like' =>'%'.$search_term.'%');
        $conditions[] = array('Mix.tag_words Like' =>'%'.$search_term.'%');
    }
    $searchResults = $this->paginate('Mix', array('Mix.published'=>1, 'OR' => $conditions));
    $this->set('searchResults', $searchResults);
}
}

That works for the first page of results but in my view I have the previous/next helpers and clicking to go to page two just brings up an empty page. How am I meant to keep the search term and be able to go to page two?

1
You should probably use a 'get' form to perform the search and include that in the pagination links. I may have an example somewhere, but not at my computer at the moment - thaJeztah

1 Answers

0
votes

The Paginator::sort method uses a get to pass the parameters, you should pass the search term as GET then in your view, before you call the pagination links try this:

$this->Paginator->options = array(
    'url' => $this->passedArgs
);

That should have your search term persist with your pagination links.