CodeIgniter Forums

Full Version: Best practice for pagination and storing search parameters
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Samuurai[/eluser]
Hi,

I'm trying to paginate and sort results after performing a search.
I've used my URI to specify result parameters which looks like this:
Code:
/user/10/asc/first_name

//which translates to this:

/controller/<start_number>/<asc or desc>/<field_name>

However, how about if I throw a search string into the mix? My search string will be an array of different skills plus a start and end date.

Post variables are clunky in this instance.. ideally it should be get variables, but I'm wondering how I should tackle this one using the CI ethos. I thought about using a session to store the search params, but ideally I want the page number to be bookmarkable.

Cheers!

El Forum

[eluser]richzilla[/eluser]
Why dont you store your search variables in the session still, and then in your controller constructor, check to see if your second uri segment is a valid page number. If it is, you can then load the search variables stored in your session, and display the page correctly filtered.

El Forum

[eluser]Samuurai[/eluser]
Thanks, I thought of doing that, but as I said, that wouldn't be bookmarkable which isn't ideal.

El Forum

[eluser]n0xie[/eluser]
Or use querystring, which is much simpler...

El Forum

[eluser]richzilla[/eluser]
Ahh, you want to be able to bookmark the search aswell?

El Forum

[eluser]Samuurai[/eluser]
[quote author="n0xie" date="1272573079"]Or use querystring, which is much simpler...[/quote]

I had no idea that existed.. awesome, thanks.

El Forum

[eluser]ccachor[/eluser]
I wouldn't bother with a session or using post/get. Basically, your controller should have a search method that takes key => value pairs to search using uri_to_assoc() helper function in the URL helper. This method works just like a get except you've got a pretty url and a dynamic search. It's a pain to keep track of uri segments, especially if you've linked the pages up statically in too many places. That's what I've found to work best. When you perform a search, take each value, string it together, and set the url to your search. Or -- you could link with your search parameters from somewhere else if you wanted as well. Hope that helps, I can elaborate if need be.

El Forum

[eluser]WanWizard[/eluser]
key => value pairs is indeed the way to go. If you process them in a foreach(), you don't have to bother with sequence as well. /user/1/sort/asc works the same as /sort/asc/user/1.

I try to store as much as possible in the session, to avoid a long URL. Don't forget that you have to do a lot of validation on the URI segments, since the user can type them in they can be anything. Only if a user should be able to bookmark something, I use the URL to pass the value.

Storing it in the session also has the advantage that the selection can be remembered. If the user navigates away from the page, and comes back later (within the same session), sort order and filter/search value are rembered and you can present the page as it was on the last visit.