CodeIgniter Forums
Best practice for pagination and storing search parameters - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Best practice for pagination and storing search parameters (/showthread.php?tid=30012)



Best practice for pagination and storing search parameters - El Forum - 04-29-2010

[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!


Best practice for pagination and storing search parameters - El Forum - 04-29-2010

[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.


Best practice for pagination and storing search parameters - El Forum - 04-29-2010

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


Best practice for pagination and storing search parameters - El Forum - 04-29-2010

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


Best practice for pagination and storing search parameters - El Forum - 04-29-2010

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


Best practice for pagination and storing search parameters - El Forum - 04-29-2010

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

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


Best practice for pagination and storing search parameters - El Forum - 04-29-2010

[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.


Best practice for pagination and storing search parameters - El Forum - 04-30-2010

[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.