CodeIgniter Forums

Full Version: Pagination suffix & reuse_query_string problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone.

I'm having trouble with CI pagination class.
My URL looks like: http://domain.com/chapter/2?var=value#anchor, where 2 is a number of a page added by pagination class.
I want my pagination to use #anchor suffix each time when user selects another page and also I need to keep query string ?var=value.

As I can see in the manual I need to use:

Code:
$config['reuse_query_string'] = TRUE;
$config['suffix'] = '#anchor';

But when I'm trying to set config in such a way, my pagination links redirecting users to: http://domain.com/chapter/2#anchor?var=value instead of http://domain.com/chapter/2?var=value#anchor.

I can't understand why it's happening  Huh
Could anyone, please, give me an advice?

Thanks in advance!
The suffix you have added is meant to add a suffix like .html or .htm to a url and as you can see, it has been placed in the right place, it is just that it was not meant to add anchor links such as #anchor

Your URL needs to be constructed differently than the way you are currently doing it. To add an anchor to a url when outputting it in a view you could do something easy like:

PHP Code:
<a href="<?php echo site_url('my/segments'); ?>#anchor">My Link</a

I don't use these very much so there might be something better to use in the URL helper, I had a quick look but could not see anything.

Hope that helps,

Paul.

PS I just took a look at the pagination class again as I realised you might not be constructing the links and you are not. The suffix comment above was not about the suffix in the pagination class so my apologies. So this answer is probably entirely irrelevant. Sorry again.
(09-07-2016, 12:39 PM)PaulD Wrote: [ -> ]The suffix you have added is meant to add a suffix like .html or .htm to a url and as you can see, it has been placed in the right place, it is just that it was not meant to add anchor links such as #anchor

Your URL needs to be constructed differently than the way you are currently doing it. To add an anchor to a url when outputting it in a view you could do something easy like:

PHP Code:
<a href="<?php echo site_url('my/segments'); ?>#anchor">My Link</a

I don't use these very much so there might be something better to use in the URL helper, I had a quick look but could not see anything.

Hope that helps,

Paul.

Thanks a lot for your reply! Now I realise that I misunderstood the purpose of $config['suffix'] option.
But I still don't realise how can I add something to the end of automatically generated by Pagination class links?  Sad
No, I have just been taking a look at the pagination class for you and I don't think you can.

When I do pagination I always do it myself, it really is not that hard using offset in your database queries and one or two calculations for number of pages etc to achieve the same. I think I tried pagination once long ago but as with all these things, as soon as you want to do something a bit different with them you get stuck pretty quickly with what the helper wants to do. A bit like the now deprecated cart class.

I did wonder why you wanted query strings, segments, and anchors on all your links, but I am sure you have good reason. You could possibly add a class to all the links and use JS to add an anchor, but it seems like a hefty workaround just to add #anchor and probably not the way to go. I hope someone else with more experience with pagination has a solution for you.

Good luck

Paul
(09-07-2016, 01:14 PM)PaulD Wrote: [ -> ]No, I have just been taking a look at the pagination class for you and I don't think you can.

When I do pagination I always do it myself, it really is not that hard using offset in your database queries and one or two calculations for number of pages etc to achieve the same. I think I tried pagination once long ago but as with all these things, as soon as you want to do something a bit different with them you get stuck pretty quickly with what the helper wants to do. A bit like the now deprecated cart class.

I did wonder why you wanted query strings, segments, and anchors on all your links, but I am sure you have good reason. You could possibly add a class to all the links and use JS to add an anchor, but it seems like a hefty workaround just to add #anchor and probably not the way to go. I hope someone else with more experience with pagination has a solution for you.

Good luck

Paul

Thanks for you reply, Paul. I thought about JS solution as a fallback.
But I think that something is wrong with Pagination class, because $config['suffix'] option should do what I want, depending on manual.
And once again, depending on these lines from manual if I will set $config['reuse_query_string'] = TRUE:


Quote:By default your Query String arguments (nothing to do with other query string options) will be ignored. Setting this config to TRUE will add existing query string arguments back into the URL after the URI segment and before the suffix.:

taken from: http://www.codeigniter.com/user_guide/li...ation.html

the $config['suffix'] should be added after QUERY_STRING, but it doesn't.

Let's take a look on 474 line from system/libraries/Pagination.php:

Code:
// Standard segment mode.
// Generate our saved query string to append later after the page number.
if ( ! empty($get))
{
    $query_string = $query_string_sep.http_build_query($get);
    $this->suffix .= $query_string;
}

And we will see $this->suffix .= $query_string; instead of $this->suffix = $query_string . $this->suffix;

Is that a bug, or I simply don't understand this thing?
Could somebody help me, please?
No, that bit is not the right place to look. That is for a standard segment url. It is the first clause of the above "if" is where your string is being generated (I think! Not an expert).

Above that is the clause that applies with the query string, and I cannot see where the suffix is added. I gave up after that point as I thought it was a long shot anyway, and because I thought someone else might have an input on it for you that knows better than me. If you look later it removes and then re-adds the query string in a most peculiar manner.
(09-09-2016, 04:50 PM)PaulD Wrote: [ -> ]No, that bit is not the right place to look. That is for a standard segment url. It is the first clause of the above "if" is where your string is being generated (I think! Not an expert).

Above that is the clause that applies with the query string, and I cannot see where the suffix is added. I gave up after that point as I thought it was a long shot anyway, and because I thought someone else might have an input on it for you that knows better than me. If you look later it removes and then re-adds the query string in a most peculiar manner.

I'm pretty sure, that I specified the right place to look, because I set application/config/config.php $config['enable_query_strings'] = FALSE; and pagination library $config['page_query_string'] = FALSE; so my string is being generated on lines 471 - 476 of system/libraries/Pagination.php

Everything becomes OK when I change that 474 line, but I don't wanna do this without official permission  Big Grin
The suffix is used to add a file type extention like .html to all your urls. Normaly you would want a url like /home.html?var=value#anchor where in this case home is the name of your controller. So I believe that "$this->suffix .= $query_string;" is correct.

My guess here is that you use the query string variable to filer/sort your data? If so perhaps you could put those variables as parameters to your method function. you will get somewhat prettier urls as well.
Or perhaps store the sort value in the users session.

But perbaps it's just easier to just generate the pagination links without your suffics and inject them afterwards.
PHP Code:
$pagination $this->pagination->create_links();
$pagination str_replace('</a>''#anchor</a>'$pagination);