Monday, April 29, 2013

Re: Problem with Pagination of search results

Just having another look at your code that is using sessions...

It looks as though when someone submits the form that contains the 'search' field, you are saving that to the session, but then checking for it's existence, reading it straight back out, and never doing the paginate.

Here is snippet of your code, modified to make better use of the session for storing the search term.  I've kept the session name the same, however, I would recommend calling it user_search, user_filter or just search, instead of users, since you're not actually storing users in the session.

public function search() { 
                // if form submitted, save search keyword to the session
                if (!empty($this -> data)) { 
                        $keyword = $this->data['User']['search']; 
                        $this->Session->write('users', $keyword); 
                }

                // if we have a search keyword, set paginate conditions
                $conditions = array('1 = 1');
                if ($this -> Session -> check('users')) { 
                    $keyword = $this -> Session -> read('users'); 
                    $conditions['or'] = array(array('User.first_name LIKE' => $keyword . '%'),array('User.last_name LIKE' => $keyword . '%'));
                } 

                // always do a paginate
                $this->paginate = array('User' => array('conditions' => $conditions, 'limit' => 5));
                $this->set('users', $this->paginate('User'));



On Tuesday, 30 April 2013 03:39:02 UTC+10, Lilit Bakunts wrote:
I only had  $this->Paginator->numbers() in view.
I'll try to make it via GET.. but after knowing how to use with sessions :)
Thanks for your reply !



--
View this message in context: http://cakephp.1045679.n5.nabble.com/Problem-with-Pagination-of-search-results-tp5714685p5714704.html
Sent from the CakePHP mailing list archive at Nabble.com.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: