php 带有获取参数的codeigniter分页网址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5384644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
codeigniter pagination url with get parameters
提问by salmane
I am having trouble setting up pagination on codeigniter when I pass parameters in the URL
我在 URL 中传递参数时在 codeigniter 上设置分页时遇到问题
if my url is like this : search/?type=groups
如果我的网址是这样的: search/?type=groups
what should be my $config['base_url']
for pagination to work?
我$config['base_url']
的分页应该是什么?
if i set the base url to search/?type=groups
the resulting url is search/?type=groups/10
如果我将基本网址设置search/?type=groups
为结果网址是search/?type=groups/10
which means $_GET['type']=groups/10
意思是 $_GET['type']=groups/10
thank you
谢谢你
采纳答案by salmane
The solution is that CodeIgniter does not function like that. what I need is a method ( in the controller ) for each one of the options in "type" so one option would be a method called :groups , another called entries etc etc each method refers to a different model class or method as needed.
解决方案是 CodeIgniter 没有那样的功能。我需要的是“类型”中每个选项的方法(在控制器中),因此一个选项是一个称为 :groups 的方法,另一个称为条目等,每个方法根据需要引用不同的模型类或方法。
I am trying to better understand OOP and CI ...a bit of adjusting to do ... feel free to comment and correct me if i am wrong. thank you
我正在努力更好地理解 OOP 和 CI ……做一些调整……如果我错了,请随时发表评论并纠正我。谢谢你
回答by Aurel
In pagination config:
在分页配置中:
if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");
Your current $_GET
vars will be shown in pagination links.
You can replace $_GET
by another associative array.
This won't add a query string unless one already exists.
您当前的$_GET
变量将显示在分页链接中。您可以替换$_GET
为另一个关联数组。这不会添加查询字符串,除非已经存在。
Update:I just saw, if you go back from another pagination number to click on the first(1), CI does not care anymore of your suffix config.
更新:我刚刚看到,如果您从另一个分页号返回单击第一个(1),CI 不再关心您的后缀配置。
To fix that use $config['first_url']
.
要解决该问题,请使用$config['first_url']
.
e.g: $config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
例如: $config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
回答by mertyildiran
The most up-to-date answer of this question is;
这个问题的最新答案是;
You should enable the reusage of the query string by enabling this configuration:
您应该通过启用此配置来启用查询字符串的重用:
$config['reuse_query_string'] = true;
after that you should initialize the pagination:
之后你应该初始化分页:
$this->pagination->initialize($config);
Added
$config['reuse_query_string']
to allow automatic repopulation of query string arguments, combined with normal URI segments. - CodeIgniter 3.0.0 Change Log
添加
$config['reuse_query_string']
以允许自动重新填充查询字符串参数,并结合正常的 URI 段。- CodeIgniter 3.0.0 更改日志
回答by King Julien
Here is my jquery solution:
这是我的 jquery 解决方案:
Just wrap pagination links in a div like this:
只需将分页链接包装在一个 div 中,如下所示:
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
than add the following jquery code:
比添加以下 jquery 代码:
$("#pagination > a").each(function() {
var g = window.location.href.slice(window.location.href.indexOf('?'));
var href = $(this).attr('href');
$(this).attr('href', href+g);
});
Works fine for me.
对我来说很好用。
回答by David Gyori
if you are using codeigniter 2 there's an option in config.php
, $config['allow_get_array']
- make sure its on TRUE.
如果你正在使用笨2有一个选项config.php
,$config['allow_get_array']
-确保其对TRUE。
Then set the pagination option $config['page_query_string']
to TRUE
.
然后将分页选项设置$config['page_query_string']
为TRUE
.
And finally, in your case set $config['base_url']
to "search/?type=groups"
, the pagination will append the per_page
query string after it.
最后,在您设置$config['base_url']
为的情况下"search/?type=groups"
,分页将在per_page
其后附加查询字符串。
It should work this way, you'll get the offset in $this->input->get("per_page")
.
它应该以这种方式工作,您将在$this->input->get("per_page")
.
code strong!
代码强!
回答by Robban
I struggled with the same issue today. My solution is this:
我今天在同样的问题上挣扎。我的解决方案是这样的:
Generate the pagination links and store them in a string (
$pagination = $this->pagination->create_links();
)Use regexp to find all links and add query strings
生成分页链接并将它们存储在字符串中 (
$pagination = $this->pagination->create_links();
)使用正则表达式查找所有链接并添加查询字符串
The regular expression code used is:
使用的正则表达式代码是:
<?php
$query = '?myvar=myvalue';
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\1[^>]*>(.*)<\/a>";
$unique = array();
if( preg_match_all("/$regexp/siU", $pagination, $matches) )
{
foreach ( $matches[2] as $link )
{
if ( !isset($unique[$link]) )
{
$data['pagination'] = str_replace($link . '"', $link . $query . '"', $data['pagination']);
$unique[$link] = '';
}
}
}
unset($unique);
Works like a charm for me! What it does is:
对我来说就像一个魅力!它的作用是:
- Find all links
- Replace unique links (since there is a previous/next links same link may appear more than once) with the original link and the query-string.
- 查找所有链接
- 用原始链接和查询字符串替换唯一链接(因为存在上一个/下一个链接,相同的链接可能会出现多次)。
Then just assign the variable to the template that will be shown and use print $your_pagination_variable_name; to show the links with your query-strings attached!
然后只需将变量分配给将显示的模板并使用 print $your_pagination_variable_name; 显示附加了查询字符串的链接!
回答by William
I think you are trying to do the same thing I was trying to do and I got it to work correctly by not setting a base url and just using this setup it kept me from having to manually editting the library
我认为您正在尝试做与我正在尝试做的相同的事情,并且通过不设置基本 url 使其正常工作,仅使用此设置即可使我不必手动编辑库
$this->load->library('pagination');
$config['use_page_numbers'] = TRUE;
$config['page_query_string'] = TRUE;
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
回答by Ken Phan
Before line:
行前:
$this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
Replace this code below:
替换下面的代码:
if(isset($_GET[$this->query_string_segment]))
{
unset($_GET[$this->query_string_segment]);
}
$uri = http_build_query($_GET);
$uri = empty($uri) ? '?' : $uri . '&';
$this->base_url = rtrim($this->base_url).$uri.$this->query_string_segment.'=';
回答by Nadeem As
回答by Ryan Herubin
Using the $config['suffix'] is the IMO best way to implement this because it doesn't require any extra processing as the regex solution. $config['suffix'] is used in the rendering of the urls in the create_links function that's part of the system/libraries/Pagination.php file so if you set the value it'll be used in the generation of the urls and won't require anymore processing which means it'll be faster.
使用 $config['suffix'] 是 IMO 实现这一点的最佳方式,因为它不需要任何额外的处理作为正则表达式解决方案。$config['suffix'] 用于在 create_links 函数中渲染 url,它是 system/libraries/Pagination.php 文件的一部分,因此如果您设置该值,它将用于生成 url 并获胜'不再需要处理,这意味着它会更快。
Thanks for the post, this saved me tons of extra, not needed, coding!
感谢您的帖子,这为我节省了大量不需要的额外编码!