Question

I'm using CI Pagination library on my website and now with $config['use_page_numbers'] set to TRUE the current page is always the first. Everything works fine, except this.

Other settings:

$config['uri_segment'] = 2;
$config['prefix'] = "p";
$route['mypage/p(:num)'] = "citate/index/$1";

This is the function that calculate the current page (the output is correct). When I'm on first page returns 1, when on third page returns 3 and so on:

function getPagCurr($offset, $limit){
   if( ! $offset) return $page = 1;
   else return ($offset/$limit)+1;
}

... though, it's not working.

I've try to set up manually, just for testing, the value of $config['cur_page'] = 2 (so this means that the second link should be considered as active) but no change at all.

CI version is latest.


LE: SOLUTION

It seems that the prefix is the problem here. With my actual configuration the link will be like this www.site.com/mypage/p2, which is not working. The working link would be www.site.com/mypage/p/2/ with the uri_segment = 3 and route mypage/p/(:num). However, I really want to have the first link structure so here's my solution (not a good one because you have to modify some system library code):

Pagination.php (start line 166):

// Set current page to 1 if using page numbers instead of offset
if ($this->use_page_numbers AND $this->cur_page == 0)
{
   $this->cur_page = $base_page;
}

..changed to:

// Set current page to 1 if using page numbers instead of offset
if ($this->use_page_numbers AND $this->cur_page == 0)
{
    $current_page = $CI->uri->segment($this->uri_segment); //get pNUM
    $current_page = substr($current_page, 1); //remove prefix
    $this->cur_page = $current_page; //set current page
}

... and now it works!

If anybody have a better solution please tell! Thanks.

Was it helpful?

Solution

Yes you are right it will not work because your segment got a p(p2)

To do this you must have to modify the core but i will say dont modify the core just extend the pagination class and modify the code with following:

Add a new class variable

var $use_rsegment       = FALSE;

Then modify the create_links() around line 157

//add
$_uri_segment = 'segment';
if($this->use_rsegment){
    $_uri_segment = 'rsegment';
}
//modify
if ($CI->uri->$_uri_segment($this->uri_segment) != $base_page)
{
     $this->cur_page = $CI->uri->$_uri_segment($this->uri_segment);

     // Prep the current page - no funny business!
     $this->cur_page = (int) $this->cur_page;
 }

The uri rsegment is the new routed segment, now set the pagination config like this

$config['use_rsegment'] = TRUE;
$this->pagination->initialize($config);

So you can use both option when ever you need. When you have route set rsegment true

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top