CodeIgniter Forums

Full Version: CI pagination with searching
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]jepotski[/eluser]
I am using the pagination library and i want to enable searching, my question is how can i pass the form data (search query) in the pages? The scenario is if I search for a field and the results contains multiple pages, how can i go to the next page and retain/pass the submitted form data?

El Forum

[eluser]mddd[/eluser]
There are different methods. You could put the search term(s) in the url, then you would get a url like
Code:
example.com/search/searchterms/2
where 2 is the page number for pagination. Another method is to do what the CI forum does: on the initial search, save the result set somewhere and make a reference to it. This would give you something like
Code:
example.com/search/6fceb89ff38bab0bcb18e7241eea0efa/2

Or you could do something in between: save the result set and link it to the search parameters. That way you keep a nice url, that people can bookmark, but still have the advantage of quick page switching because you have the results saved already. In that case, the system knows that there already was a search for "searchterms" and it can get the results back for quick access.

You should consider the circumstances too. How long do you want to keep the results cached. For instance, on a busy forum there can quickly be new messages containing the keywords. So you should not cache the information for too long. Or keep a result set for every user separately. That way user A can browse their results for some time, and user B who searches the same terms 10 minutes later still gets a new set of results. This takes more queries on the database but has the advantage of a fresh, up-to-date query for every new search a user does.

El Forum

[eluser]jepotski[/eluser]
[quote author="mddd" date="1275399453"]There are different methods. You could put the search term(s) in the url, then you would get a url like
Code:
example.com/search/searchterms/2
where 2 is the page number for pagination. Another method is to do what the CI forum does: on the initial search, save the result set somewhere and make a reference to it. This would give you something like
Code:
example.com/search/6fceb89ff38bab0bcb18e7241eea0efa/2

Or you could do something in between: save the result set and link it to the search parameters. That way you keep a nice url, that people can bookmark, but still have the advantage of quick page switching because you have the results saved already. In that case, the system knows that there already was a search for "searchterms" and it can get the results back for quick access.

You should consider the circumstances too. How long do you want to keep the results cached. For instance, on a busy forum there can quickly be new messages containing the keywords. So you should not cache the information for too long. Or keep a result set for every user separately. That way user A can browse their results for some time, and user B who searches the same terms 10 minutes later still gets a new set of results. This takes more queries on the database but has the advantage of a fresh, up-to-date query for every new search a user does.[/quote]

Where should I save the searchterms, to the database or session? how do you get the characters:

Quote:6fceb89ff38bab0bcb18e7241eea0efa

is it encoded? Is it applicable if i have to search many fields? thanks

El Forum

[eluser]bhogg[/eluser]
Where you save it would depend on your application, if you want logs of it, persistence, etc. That's likely the key in the database (hash of the contents, perhaps), else could put it in a cookie, or session var.