简体   繁体   English

CakePHP分页和获取参数

[英]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. 当用户搜索某些内容时,我有一个像domain.com/search/?s=keyword这样的网址,但是paginator会给我一些像domain.com/search/page:x这样的链接,所以在下一个和prev和数字页面中,get参数会丢失。 I need to configure paginator to get links like domain.com/search/page:x/?s=keyword But I can't do this. 我需要配置paginator来获取像domain.com/search/page:x/?s=keyword这样的链接但是我不能这样做。

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. 然后你可以在保留GET变量的同时使用paginator helper。

hope that it helped :) 希望它有帮助:)

to make it easier you can putl $paginator options in your view or .ctp file 为了方便您在视图或.ctp文件中使用putl $ paginator选项

$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 要将所有URL参数传递给paginator函数,请将以下内容添加到视图中:纯文本视图

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

That's it. 而已。 See http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html 请参阅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. 有些时候$this->passedArgs没有工作,在那里传递静态值并尝试使用该静态值。

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

I used Matyas solution, but for $keyword I did like this: 我使用了Matyas解决方案,但对于$ keyword,我确实喜欢这样:

$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: 在app_controller文件中:

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']];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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