Welcome Guest, Not a member yet? Register   Sign In
Pagination Problem
#1

[eluser]CodeIgniterNewbie[/eluser]
I do not want to use URI segments for my pagination (as I have a lot of possible parameters to pass in unknown order). Currently, I have $config['enable_query_strings'] = FALSE;, and if I set it to TRUE, I get something like this (from CI's example):

http://example.com/index.php?c=test&m=page&per_page=20

Where "c" is the controller, "m" is the method, etc. This is NOT what I want.

I want something like this:

http://domain.com/?min_amount=100&max_am...offset=100

Is there a way to achieve this in CI? If not, is there some library already available that I can use?
#2

[eluser]yacman[/eluser]
Enabling query strings is not what you want to do.

All you need to do to access GET variables in your controller is:
Code:
function index() {
    $min_amount= $this->input->get('min_amount');
    $max_amount= $this->input->get('max_amount');
    $color=$this->input->get('blue');
    $size=$this->input->get('size');
    //If there is no limit, default to 20
    $limit=$this->input->get('limit',20);
    $offset=$this->input->get('offset',20);
}
#3

[eluser]CodeIgniterNewbie[/eluser]
Hi yacman, can you please show me how you would initialize the pagination class to get the output I need? Thanks.
#4

[eluser]yacman[/eluser]
Don't copy and paste this because it probably won't work, but it's a start:
Code:
function index() {

    // configure the paginator
    $this->load->library('pagination');

    $min_amount= $this->input->get('min_amount');
    $max_amount= $this->input->get('max_amount');
    $color=$this->input->get('blue');
    $size=$this->input->get('size');
    //If there is no limit, default to 20
    $limit=$this->input->get('limit',20);
    $page =$this->input->get('page',1);

    $config['base_url'] = current_url().'?page='.$page;
    $config['per_page'] = $limit;
    $config['page_query_string'] = true;
    $offset = ($page-1) * $limit;
    $config['total_rows'] = $this->db->count_all()->from('a_table')->where('color', $color)->where('size',$size);

    $paged_results = $this->db->from('a_table')->where('color', $color)->where('size',$size)->limit($limit, $offset);

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

      echo $this->pagination->create_links();

}
#5

[eluser]CodeIgniterNewbie[/eluser]
yacman,

I've simplified your code:

Code:
// configure the paginator
$this->load->library('pagination');

// these aren't in the query string
$min_amount = '25';
$max_amount = '100';
$color      = 'blue';
$size       = 'large';

$limit      = '20';
$page       = '1';

$config['base_url']          = current_url() . '?page=' . $page;
$config['per_page']          = $limit;
$config['page_query_string'] = TRUE;
//$offset                      = ($page - 1) * $limit;
$config['total_rows']        = '120';

// $paged_results = $this->db->from('a_table')->where('color', $color)->where('size', $size)->limit($limit, $offset);

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

Notice the problems (see comments). The problem is that the GET params such color and size aren't in the URL.
#6

[eluser]yacman[/eluser]
Add each paramater you wish to persist in the Pagination links onto the $config['base_url'] .
#7

[eluser]CodeIgniterNewbie[/eluser]
Ah, so something like this:

Code:
$config['base_url']   = base_url() . 'search/?' . http_build_query($_GET, '', '&');

Just make sure the $_GET array does not have "per_page" in it because the pagination library will handle that?
#8

[eluser]CodeIgniterNewbie[/eluser]
yacman,

I think I'm still confused. Please take a look at the following code in a controller named "test":

Code:
$this->load->library('pagination');

// These 3 are the minimum config that needs to be set
$config['base_url']   = base_url() . 'test/?' . http_build_query($_GET, '', '&');
$config['total_rows'] = 173;
$config['per_page']   = 20;

// This returns something like this: ?name=foo/20
// I would expect something like this: ?name=foo&per_page=20
// $config['enable_query_strings'] = TRUE;

// This retuns something like this: ?name=foo&per_page=20 (which is what I want),
// but the documentation says it should be: ?c=test&m=index&per_page=20
$config['page_query_string'] = TRUE;

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

Then go to: test/?name=foo

The resulting pagination links look correct. But when you click on, say, link number "2", you are redirected to
Code:
?name=foo&per_page=20
which is correct. However, the new pagination created looks wrong. It looks something like this:
Code:
?name=foo&per_page=20&per_page=40
(where per_page appears twice in the query string).

What's going on?




Theme © iAndrew 2016 - Forum software by © MyBB