codeigniter pagination url with get parameters

2019-01-21 03:30发布

问题:

I am having trouble setting up pagination on codeigniter when I pass parameters in the URL

if my url is like this : search/?type=groups

what should be my $config['base_url'] for pagination to work?

if i set the base url to search/?type=groups the resulting url is search/?type=groups/10

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

thank you

回答1:

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.

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.

To fix that use $config['first_url'].

e.g: $config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);



回答2:

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



回答3:

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

Then set the pagination option $config['page_query_string'] to 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.

It should work this way, you'll get the offset in $this->input->get("per_page").

code strong!



回答4:

Here is my jquery solution:

Just wrap pagination links in a div like this:

$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';

than add the following jquery code:

$("#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:

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

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

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. 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!



回答6:

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


回答7:

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.

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



回答8:

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.

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



回答9:

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:

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

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


回答11:

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>

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:

IN YOUR CONTROLLER:

$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:

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.



回答14:

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:

base_url() . /search/?term=

So, here is what I have done:

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


回答15:

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