Pagination with search results in cakephp

I have implemented a user model, and I have a simple search form for searching by gender, age, location. I use paginator to paginate the results. The problem is that it only remembers the search criteria on the first page. When I click on page 2 or etc. By default, it returns to the search for all users.

Here is my dirty search code in my controller, basically it just checks the submitted form fields and makes a request in the correspondence field in the Users table, and then paginates the results:

if (!empty($this->data)) { // by name if (!empty($this->data['User']['search_name'])) { $this->paginate = array('conditions' => array('visible'=>1, 'OR'=>array( 'User.username LIKE' => '%'.$this->data['User']['search_name'].'%', 'User.firstname LIKE' => '%'.$this->data['User']['search_name'], 'User.lastname LIKE' => '%'.$this->data['User']['search_name']) ), 'limit'=>'10', 'order'=>'User.username'); } // by gender else if (!empty($this->data['User']['search_gender'])) { $this->paginate = array('conditions' => array( 'visible'=>1, 'User.gender' => $this->data['User']['search_gender'] ), 'limit'=>'10', 'order'=>'User.username'); } // by state else if (!empty($this->data['User']['search_state'])) { $this->paginate = array('conditions' => array( 'visible'=>1, 'User.state' => $this->data['User']['search_state'] ), 'limit'=>'10', 'order'=>'User.username'); } // Send the results for the above criteria to the view $results = $this->paginate('User'); $this->set('users', $results); } // Default retrieval of all users else { $this->paginate = array('conditions'=>array('visible'=>1), 'limit'=>'10', 'order'=>'User.username'); $this->set('users', $this->paginate('User')); } 

I am trying to figure out how to make subsequent pages pagination to remember my search criteria. Thanks for any help.

+4
source share
1 answer

$this->data empty on page 2 because it is populated by posting your search form (which does not go to page two).

Change your search form message to GET ( $this->Form->create('ModelName', array('type'=>'get') ) and parse $this->params instead of $this->data

+7
source

Source: https://habr.com/ru/post/1444411/


All Articles