CodeIgniter Forums

Full Version: How to keep using search results data through various functions and controllers?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]deMonkey[/eluser]
Here's what I have and need:
- Site shows 1500 rows from the table 'cars' using the controller /cars
- User can click the horsepower/brand/etc. tab at the table header of the cars and sort by those using the controller /sort (sets session variable then redirects to /cars)
- User clicks "Ford", a brand name of cars and sees only 200 cars (Fords) running the function /cars/search/$brand
- Now the user needs to sort this data without resetting the search query

I made a function inside my base controller called 'search' that modifies the original query to show $this->db->where('brand_name', $brand); and this works fine. If the user goes to /cars/search/Ford they will only see cars by Ford.

It was recommended to me that I use session-based sorting, so I created another controller at /sort/ that manages the various options (/sort/weight/, /sort/horsepower/, and so-on). This also works fine, but there's one problem.

Inside the sort controller, I had to redirect back to the cars controller, so I used redirect('cars'); This sorts the data as needed, but returns to the default cars controller and hence all 1500 cars. How can I determine if a search has been made, or redirect to the exact function that was being used (with its parameters) prior to running the new controller? I need another way to hang onto this query as a parameter of a sort function, but don't really understand function parameters in CI.

Additionally I wrote a search that narrows it down to specific years, but the searches are mutually exclusive because I can't really think of a good way to search for year AND brand given that they require different functions.

I made all this work perfectly with sessions, but the problem is that hitting BACK on your browser will keep the stored search results (since the brand name and year are session variables) and so you had to close them out one at a time or click a reset button. I want to maintain browser functionality AND the sorting.

I'm a newbie with PHP and MySQL, and just started using CodeIgniter this week, so I apologize if this does not make sense but I'd be glad to explain the algorithm or end-goal if you have questions. Any tutorials that cover this sort of stuff would be GREATLY appreciated.

El Forum

[eluser]Developer13[/eluser]
Why not put a sort method inside your cars controller instead of having a separate controller for sorting?

El Forum

[eluser]deMonkey[/eluser]
That's probably a good first step. Big Grin

I made that change. Now it's /cars/sort/name etc. to re-sort. I'll reply here after re-evaluating the situation.

El Forum

[eluser]glemigh[/eluser]
While what I was doing is not exactly the same, I just used cookies to pass that kind of data around.

George

El Forum

[eluser]deMonkey[/eluser]
I made it work how I wanted it to.

All I did was add a few parameters to the methods to keep track of the current search so it wouldn't belost when switching stuff around.

El Forum

[eluser]ztinger[/eluser]
I added the search vars to the session. It works good. Wink