Nowadays, it has rather become a trend to let the visitors sort or filter search results. We let them search by the bestsellers, popular, newest, or, simply by price. We can use a form with a select box having options and let user hit the submit button. You can get the first result page to work pretty well with your default CakePHP setup. But the pagination fails. It is because Cake does not save the form post data. And here is a simple trick I found somewhere in the Internet.
USE SESSION VARIABLES.
In your controller setup:
function my_function() {
if(!empty($this->data)){
$this ->Session->write('search',$this->data['Model']['field']);
$search_string =$this->data['Model']['field'];
} else {
$search_string =$this ->Session->read('search');
}
$condition = array('Model.field_name'=>$search_string);
$search_data = $this->Model->find('all',array('conditions'=>$condition));
$this->set('search_data',$search_data);
}
/* in your view file use */
debug($search_data);
/* you can see the recordset, if debug mode is set to 2 or above. */
Hope this helps someone else as well.
Happy baking.
USE SESSION VARIABLES.
In your controller setup:
function my_function() {
if(!empty($this->data)){
$this ->Session->write('search',$this->data['Model']['field']);
$search_string =$this->data['Model']['field'];
} else {
$search_string =$this ->Session->read('search');
}
$condition = array('Model.field_name'=>$search_string);
$search_data = $this->Model->find('all',array('conditions'=>$condition));
$this->set('search_data',$search_data);
}
/* in your view file use */
debug($search_data);
/* you can see the recordset, if debug mode is set to 2 or above. */
Hope this helps someone else as well.
Happy baking.
No comments:
Post a Comment