Re: How to Paginate Search Results?
Thanks guys. Thank you Andras Kende for recommending that plugin. The Cakedc search plugin is AMAZING. So easy. I had a bunch of lines of code written myself to do the search and it was barely working. I exchanged all of my ugly code out and used like maybe 15 lines of code to implement that plugin and it works great. Thanks Cakedc! Andrew On Oct 29, 2:15 pm, Andras Kende wrote: > You can do it either with PRG (post redirect get) concept: > > http://bakery.cakephp.org/articles/luciansabo/2011/08/12/post_redirec...http://cakedc.com/downloads/view/cakephp_search_plugin > > or with write the posted form data to a session once and keep reading it > through pagination > > something like: > > if(!empty($this->data['User']['search_name'])) { > $this->Session->write('conditions.firstname', > $thisis->data['User']['search_name']); > } > > if($this->Session->check('conditions')) { > $conditions = $this->Session->read('conditions'); > } else { > $conditions = null > } > > $this->paginate = array( > 'recursive' => -1, > 'conditions' => $conditions, > > Andras Kendehttp://www.kende.com > > On Oct 29, 2011, at 1:27 PM, andrewperk wrote: > > > > > > > > > Hello, > > > I've implemented a user model and I have a simple search form for > > searching by gender, age, location. I'm using the paginator to > > paginate the results. The problem is that it's only remembering the > > search criteria on the first page. When I click on page 2, or next > > etc.. It defaults back to searching all users. > > > Here's my dirty search code in my controller, basically it just checks > > the submitted form fields and does a query on the matching 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'm trying to figure out how to make subsequent pages of the > > pagination remember my search criteria. Thanks for any help. > > > -- > > Our newest site for the community: CakePHP Video > > Tutorialshttp://tv.cakephp.org > > Check out the new CakePHP Questions sitehttp://ask.cakephp.organd help > > others with their CakePHP related questions. > > > To unsubscribe from this group, send email to > > cake-php+unsubscr...@googlegroups.com For more options, visit this group > > athttp://groups.google.com/group/cake-php -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: How to Paginate Search Results?
In recent projects I use CakeDC's searchable plugin, but before that I simply did the following in my view: if (!empty($this->passedArgs)) { $this->Paginator->options(array('url' => $this->passedArgs)); } HTH, Paul On Oct 29, 9:15 pm, Andras Kende wrote: > You can do it either with PRG (post redirect get) concept: > > http://bakery.cakephp.org/articles/luciansabo/2011/08/12/post_redirec...http://cakedc.com/downloads/view/cakephp_search_plugin > > or with write the posted form data to a session once and keep reading it > through pagination > > something like: > > if(!empty($this->data['User']['search_name'])) { > $this->Session->write('conditions.firstname', > $thisis->data['User']['search_name']); > } > > if($this->Session->check('conditions')) { > $conditions = $this->Session->read('conditions'); > } else { > $conditions = null > } > > $this->paginate = array( > 'recursive' => -1, > 'conditions' => $conditions, > > Andras Kendehttp://www.kende.com > > On Oct 29, 2011, at 1:27 PM, andrewperk wrote: > > > > > > > > > Hello, > > > I've implemented a user model and I have a simple search form for > > searching by gender, age, location. I'm using the paginator to > > paginate the results. The problem is that it's only remembering the > > search criteria on the first page. When I click on page 2, or next > > etc.. It defaults back to searching all users. > > > Here's my dirty search code in my controller, basically it just checks > > the submitted form fields and does a query on the matching 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'm trying to figure out how to make subsequent pages of the > > pagination remember my search criteria. Thanks for any help. > > > -- > > Our newest site for the community: CakePHP Video > > Tutorialshttp://tv.cakephp.org > > Check out the new CakePHP Questions sitehttp://ask.cakephp.organd help > > others with their CakePHP related questions. > > > To unsubscribe from this group, send email to > > cake-php+unsubscr...@googlegroups.com For more options, visit this group > > athttp://groups.google.com/group/cake-php -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: How to Paginate Search Results?
You can do it either with PRG (post redirect get) concept: http://bakery.cakephp.org/articles/luciansabo/2011/08/12/post_redirect_get_design_pattern_component http://cakedc.com/downloads/view/cakephp_search_plugin or with write the posted form data to a session once and keep reading it through pagination something like: if(!empty($this->data['User']['search_name'])) { $this->Session->write('conditions.firstname', $thisis->data['User']['search_name']); } if($this->Session->check('conditions')) { $conditions = $this->Session->read('conditions'); } else { $conditions = null } $this->paginate = array( 'recursive' => -1, 'conditions' => $conditions, Andras Kende http://www.kende.com On Oct 29, 2011, at 1:27 PM, andrewperk wrote: > Hello, > > I've implemented a user model and I have a simple search form for > searching by gender, age, location. I'm using the paginator to > paginate the results. The problem is that it's only remembering the > search criteria on the first page. When I click on page 2, or next > etc.. It defaults back to searching all users. > > Here's my dirty search code in my controller, basically it just checks > the submitted form fields and does a query on the matching 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'm trying to figure out how to make subsequent pages of the > pagination remember my search criteria. Thanks for any help. > > -- > Our newest site for the community: CakePHP Video Tutorials > http://tv.cakephp.org > Check out the new CakePHP Questions site http://ask.cakephp.org and help > others with their CakePHP related questions. > > > To unsubscribe from this group, send email to > cake-php+unsubscr...@googlegroups.com For more options, visit this group at > http://groups.google.com/group/cake-php -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: How to Paginate Search Results?
I am also interested in this answer. I just started learning cakephp, and 2.0 is what I'm starting on. If there is an example app I can download with a database and browse it's code, I would be appreciative of that as well. I had the same complication as andrew, and I did it another way, and it was creating two queries, one with the search filter and one without it. On Sat, Oct 29, 2011 at 1:27 PM, andrewperk wrote: > Hello, > > I've implemented a user model and I have a simple search form for > searching by gender, age, location. I'm using the paginator to > paginate the results. The problem is that it's only remembering the > search criteria on the first page. When I click on page 2, or next > etc.. It defaults back to searching all users. > > Here's my dirty search code in my controller, basically it just checks > the submitted form fields and does a query on the matching 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'm trying to figure out how to make subsequent pages of the > pagination remember my search criteria. Thanks for any help. > > -- > Our newest site for the community: CakePHP Video Tutorials > http://tv.cakephp.org > Check out the new CakePHP Questions site http://ask.cakephp.org and help > others with their CakePHP related questions. > > > To unsubscribe from this group, send email to > cake-php+unsubscr...@googlegroups.com For more options, visit this group > at http://groups.google.com/group/cake-php > -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
How to Paginate Search Results?
Hello, I've implemented a user model and I have a simple search form for searching by gender, age, location. I'm using the paginator to paginate the results. The problem is that it's only remembering the search criteria on the first page. When I click on page 2, or next etc.. It defaults back to searching all users. Here's my dirty search code in my controller, basically it just checks the submitted form fields and does a query on the matching 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'm trying to figure out how to make subsequent pages of the pagination remember my search criteria. Thanks for any help. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php