CodeIgniter Forums
How to keep using search results data through various functions and controllers? - 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: How to keep using search results data through various functions and controllers? (/showthread.php?tid=2185)



How to keep using search results data through various functions and controllers? - El Forum - 07-20-2007

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


How to keep using search results data through various functions and controllers? - El Forum - 07-20-2007

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


How to keep using search results data through various functions and controllers? - El Forum - 07-20-2007

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


How to keep using search results data through various functions and controllers? - El Forum - 07-20-2007

[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


How to keep using search results data through various functions and controllers? - El Forum - 07-20-2007

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


How to keep using search results data through various functions and controllers? - El Forum - 07-21-2007

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