[ACCEPTED]-codeigniter pagination url with get parameters-codeigniter

Accepted answer
Score: 104

In pagination config:

if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");

Your current $_GET vars 7 will be shown in pagination links. You can 6 replace $_GET by another associative array. This 5 won't add a query string unless one already 4 exists.

Update: I just saw, if you go back from 3 another pagination number to click on the 2 first(1), CI does not care anymore of your 1 suffix config.

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

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

Score: 30

The most up-to-date answer of this question 6 is;

You should enable the reusage of the 5 query string by enabling this configuration:

$config['reuse_query_string'] = true;

after 4 that you should initialize the pagination:

$this->pagination->initialize($config);

Added 3 $config['reuse_query_string'] to allow automatic repopulation of query 2 string arguments, combined with normal URI 1 segments. - CodeIgniter 3.0.0 Change Log

Score: 5

Here is my jquery solution:

Just wrap pagination 2 links in a div like this:

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

than add the following 1 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.

Score: 4

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

Then 5 set the pagination option $config['page_query_string'] to TRUE.

And finally, in 4 your case set $config['base_url'] to "search/?type=groups", the pagination will 3 append the per_page query string after it.

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

code strong!

Score: 2

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

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

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

The 5 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 4 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 3 assign the variable to the template that 2 will be shown and use print $your_pagination_variable_name; to 1 show the links with your query-strings attached!

Score: 2

I think you are trying to do the same thing 4 I was trying to do and I got it to work 3 correctly by not setting a base url and 2 just using this setup it kept me from having 1 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); 
Score: 1

Using the $config['suffix'] is the IMO best 10 way to implement this because it doesn't 9 require any extra processing as the regex 8 solution. $config['suffix'] is used in 7 the rendering of the urls in the create_links 6 function that's part of the system/libraries/Pagination.php 5 file so if you set the value it'll be used 4 in the generation of the urls and won't 3 require anymore processing which means it'll 2 be faster.

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

Score: 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.'=';

0

Score: 1

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

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

The solution is that CodeIgniter does not 10 function like that. what I need is a method 9 ( in the controller ) for each one of the 8 options in "type" so one option would be 7 a method called :groups , another called 6 entries etc etc each method refers to a 5 different model class or method as needed.

I 4 am trying to better understand OOP and CI 3 ...a bit of adjusting to do ... feel free 2 to comment and correct me if i am wrong. thank 1 you

Score: 0

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

var $get='';

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

Now in the controller 2 or model input these line

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

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

Score: 0

IN YOUR CONTROLLER:

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

IN 1 YOUR VIEW:

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

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

Score: 0

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

My URI was to be something like 1 this:

base_url() . /search/?term=

So, here is what I have done:

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

More Related questions