带有get参数的codeigniter分页url

时间:2022-07-15 10:42:35

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

如果我将基本网址设置为搜索/?type = groups,则生成的网址为search /?type = groups / 10

which means $_GET['type']=groups/10

这意味着$ _GET ['type'] = groups / 10

thank you

谢谢

15 个解决方案

#1


97  

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);

#2


10  

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更改日志

#3


4  

if you are using codeigniter 2 there's an option in config.php, $config['allow_get_array'] - make sure its on TRUE.

如果你使用的是codeigniter 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!

代码强!

#4


4  

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.

对我来说很好。

#5


2  

I struggled with the same issue today. My solution is this:

我今天在同一个问题上挣扎。我的解决方案是:

  1. Generate the pagination links and store them in a string ( $pagination = $this->pagination->create_links(); )

    生成分页链接并将它们存储在一个字符串中($ pagination = $ this-> pagination-> create_links();)

  2. Use regexp to find all links and add query strings

    使用regexp查找所有链接并添加查询字符串

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:

对我来说就像一个魅力!它的作用是:

  1. Find all links
  2. 查找所有链接
  3. 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.
  4. 使用原始链接和查询字符串替换唯一链接(因为前一个/下一个链接可能不止一次出现相同的链接)。

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;显示附加查询字符串的链接!

#6


1  

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不起作用。我需要的是“类型”中每个选项的方法(在控制器中),因此一个选项将是一个方法:组,另一个称为条目等等,每个方法根据需要引用不同的模型类或方法。

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 ...稍微调整一下...如果我错了,请随意评论并纠正我。谢谢

#7


1  

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文件的一部分,所以如果设置了它,它将用于生成urls并赢取不再需要处理,这意味着它会更快。

Thanks for the post, this saved me tons of extra, not needed, coding!

感谢帖子,这为我节省了大量额外的,不需要的编码!

#8


1  

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

我认为你正在尝试做我想做的事情,我通过不设置基本网址让它正常工作,只是使用这个设置,它让我不必手动编辑图书馆

$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); 

#9


1  

Before line:

排队前:

$this->base_url = rtrim($this->base_url).'&amp;'.$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 . '&amp;';
$this->base_url = rtrim($this->base_url).$uri.$this->query_string_segment.'=';

#10


1  

Just see this link.
Just update the modified pagination class and then add

只需看到这个链接。只需更新修改后的分页类,然后添加即可

$config['get'] = "?string=" . $_REQUEST['string']."&searchBy=" . $_REQUEST['searchBy']; 

#11


0  

I got the same problem. My solution is to modify Pagination and put it in application/libraries. First i create

我遇到了同样的问题。我的解决方案是修改分页并将其放在应用程序/库中。首先我创造

var $get='';

and find all "a" elements and add $get in the href="........'.$this->get.'"'>.......</a>

并找到所有“a”元素并在href =“........'中添加$ get。$ this-> get。'”'> .......

Now in the controller or model input these line

现在在控制器或模型输入这些行

$config['get']=(isset($_GET['search']))?'?search='.$_GET['search']:'';

That's it! i hope it will help you.

而已!我希望它会对你有所帮助。

#12


0  

IN YOUR CONTROLLER:

在你的控制器中:

$config['anchor_class'] = "class='link-pagination'";

$ config ['anchor_class'] =“class ='link-pagination'”;

IN YOUR VIEW:

在你的视图中:

$(".link-pagination").each(function() {
   $(this).attr("href", $(this).attr('href') + "?id=<?= $this->input->get('id') ?>");
});

#13


0  

Had the same problem but realized that newer versions of CI have a suffix defined within the pagination class. Though it's still not in the documentation. No need to hack it.

有同样的问题,但意识到更新版本的CI在分页类中定义了后缀。虽然它仍然没有在文档中。不需要破解它。

#14


0  

I have encountered with similar kind of problem, and based on the above answer here is what I have to do for customization according to my needs.

我遇到过类似的问题,基于上面的答案,这里是我根据自己的需要做的定制。

My URI was to be something like this:

我的URI是这样的:

base_url() . /search/?term=

So, here is what I have done:

所以,这就是我所做的:

$config['base_url'] = base_url ."/search/?term=" . $_GET['term']

#15


-4  

Hope you'll find the answer on this page: LINK. Edit your answer back in if you can.

希望你能在这个页面找到答案:LINK。如果可以,请重新编辑您的答案。

#1


97  

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);

#2


10  

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更改日志

#3


4  

if you are using codeigniter 2 there's an option in config.php, $config['allow_get_array'] - make sure its on TRUE.

如果你使用的是codeigniter 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!

代码强!

#4


4  

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.

对我来说很好。

#5


2  

I struggled with the same issue today. My solution is this:

我今天在同一个问题上挣扎。我的解决方案是:

  1. Generate the pagination links and store them in a string ( $pagination = $this->pagination->create_links(); )

    生成分页链接并将它们存储在一个字符串中($ pagination = $ this-> pagination-> create_links();)

  2. Use regexp to find all links and add query strings

    使用regexp查找所有链接并添加查询字符串

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:

对我来说就像一个魅力!它的作用是:

  1. Find all links
  2. 查找所有链接
  3. 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.
  4. 使用原始链接和查询字符串替换唯一链接(因为前一个/下一个链接可能不止一次出现相同的链接)。

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;显示附加查询字符串的链接!

#6


1  

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不起作用。我需要的是“类型”中每个选项的方法(在控制器中),因此一个选项将是一个方法:组,另一个称为条目等等,每个方法根据需要引用不同的模型类或方法。

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 ...稍微调整一下...如果我错了,请随意评论并纠正我。谢谢

#7


1  

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文件的一部分,所以如果设置了它,它将用于生成urls并赢取不再需要处理,这意味着它会更快。

Thanks for the post, this saved me tons of extra, not needed, coding!

感谢帖子,这为我节省了大量额外的,不需要的编码!

#8


1  

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

我认为你正在尝试做我想做的事情,我通过不设置基本网址让它正常工作,只是使用这个设置,它让我不必手动编辑图书馆

$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); 

#9


1  

Before line:

排队前:

$this->base_url = rtrim($this->base_url).'&amp;'.$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 . '&amp;';
$this->base_url = rtrim($this->base_url).$uri.$this->query_string_segment.'=';

#10


1  

Just see this link.
Just update the modified pagination class and then add

只需看到这个链接。只需更新修改后的分页类,然后添加即可

$config['get'] = "?string=" . $_REQUEST['string']."&searchBy=" . $_REQUEST['searchBy']; 

#11


0  

I got the same problem. My solution is to modify Pagination and put it in application/libraries. First i create

我遇到了同样的问题。我的解决方案是修改分页并将其放在应用程序/库中。首先我创造

var $get='';

and find all "a" elements and add $get in the href="........'.$this->get.'"'>.......</a>

并找到所有“a”元素并在href =“........'中添加$ get。$ this-> get。'”'> .......

Now in the controller or model input these line

现在在控制器或模型输入这些行

$config['get']=(isset($_GET['search']))?'?search='.$_GET['search']:'';

That's it! i hope it will help you.

而已!我希望它会对你有所帮助。

#12


0  

IN YOUR CONTROLLER:

在你的控制器中:

$config['anchor_class'] = "class='link-pagination'";

$ config ['anchor_class'] =“class ='link-pagination'”;

IN YOUR VIEW:

在你的视图中:

$(".link-pagination").each(function() {
   $(this).attr("href", $(this).attr('href') + "?id=<?= $this->input->get('id') ?>");
});

#13


0  

Had the same problem but realized that newer versions of CI have a suffix defined within the pagination class. Though it's still not in the documentation. No need to hack it.

有同样的问题,但意识到更新版本的CI在分页类中定义了后缀。虽然它仍然没有在文档中。不需要破解它。

#14


0  

I have encountered with similar kind of problem, and based on the above answer here is what I have to do for customization according to my needs.

我遇到过类似的问题,基于上面的答案,这里是我根据自己的需要做的定制。

My URI was to be something like this:

我的URI是这样的:

base_url() . /search/?term=

So, here is what I have done:

所以,这就是我所做的:

$config['base_url'] = base_url ."/search/?term=" . $_GET['term']

#15


-4  

Hope you'll find the answer on this page: LINK. Edit your answer back in if you can.

希望你能在这个页面找到答案:LINK。如果可以,请重新编辑您的答案。