简体   繁体   中英

CakePHP pagination and the get parameters

I have a problem with the pagination in my search page. When a user search something I have a url like domain.com/search/?s=keyword but paginator gives me links like domain.com/search/page:x , so in the next and prev and numbers page the get parameter is lost. I need to configure paginator to get links like domain.com/search/page:x/?s=keyword But I can't do this.

I need to know how to configure

$paginator->options();
$paginator->next();
$paginator->prev();
$paginator->numbers();

to get the needed efect. Thanx.

create the options array

$options = array(
    'url'=> array(
        'controller' => 'posts', 
        'action' => 'search', 
        '?' => 'keyword='.$keyword
     )
);

set it to the helper

$paginator->options($options)

and then you can use the paginator helper while retaining the GET variables.

hope that it helped :)

to make it easier you can putl $paginator options in your view or .ctp file

$this->Paginator->options['url']['?'] = $this->params['ur];

then put the value that you want :)

To pass all URL arguments to paginator functions, add the following to your view: Plain Text View

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

That's it. See http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html

use statement in your view for pass parameters while pagination

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

this is simplest way to pass parameters.

Some time $this->passedArgs not working at that time pass static value at there and try with that static value.

$paginator->options(array('url' => array('a', 'b'))); 

I used Matyas solution, but for $keyword I did like this:

$queryString = explode('?', $_SERVER['REQUEST_URI']);
$options = array('url'=> array('controller' => 'post', 'action' => 'search', '?' => $queryString[1]));

Check this comment .

I used it, it works fine.

in app_controller file:

function _paginatorURL() {
  $passed = "";
  $retain = $this->params['url'];
  unset($retain['url']);
  $this->set('paginatorURL',array($passed, '?' => http_build_query($retain)));
}

function beforeFilter()
{
  $this->_paginatorURL();
}

in views file:

  <?php $paginator->options = array( 'url' => $paginatorURL );?>

Hope it helps.

Basically you can do it like this:

function list_results($keywords)
  {
  $data = $this->paginate('Posts', array('Post.title LIKE' => '%'.$keywords.'%'));
  }

I know this is old but found a simple solution that works for me. Add following in view file -

$paginator->options(array('url' => array_merge($this->passedArgs,
array('?' => ltrim(strstr($_SERVER['QUERY_STRING'], '&'), '&')))));

Found it here

$this->Paginator->options['url']['?'] = ['key' => $this->params['url']['value']];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM