views:

592

answers:

4

I'm writing an app that allows you to filter database results based on Location and Category.

If someone was to search for Liverpool under the Golf category the URI would be /index.php/search/Liverpool/Golf.

Should someone want to search by Location but not category, they would be sent to /index.php/search/Liverpool

However, should someone want to filter only by category they would be unable to use /index.php/search/Golf because that would be caught by the location search.

Is there a best practice way to have /index.php/search/Golf be recognised? Some best practice as to what else to add to the URI to make these two queries distinct? /index.php/search/category/Golf perhaps?

Though that is beginning to show characteristics of /index.php?search&category=Golf which is exactly what I'm trying to avoid.

+2  A: 

Try using $this->uri->uri_to_assoc(n) described here http://codeigniter.com/user_guide/libraries/uri.html (half way down on page)

basically you will structure your url like this:

mysite.com/index.php/search/location/liverpool/category/golf

NOTE: the parameters are optional so you dont have to have both in there all the time. you can just as well do

mysite.com/index.php/search/location/liverpool/ and

mysite.com/index.php/search/category/golf

this way it will return FALSE if the element you are looking for does not exist

Tom Schlick
This is the best way to do it, as it maintains the key/value pairs of GET variables without the nasty syntax.
Phil Sturgeon
A: 

It would probably be best to keep your URI segments relavent no matter what they are searching for.

index.php/LOCATION/CATEGORY

If they are not interested in a location then pass a filler to the system:

index.php/anywhere/golf

Then in your code you just check for that specific string of ANYWHERE to determine if they only want to see the activity. I assume that you are going to be redirecting them with either links or forums (and that they aren't typing the URI string themselves) so you should be safe in just passing information that you expect and testing against that.

angryCodeMonkey
I'm going with this`index.php/search/All/All/Golf`
Matt
@Matt - I think you will be satisfied doing it this way.. Your uri position (1st/2nd/3rd) basically replaces the key in the old school GET string key=value pair.
angryCodeMonkey
A: 

I use the format suggested by Tom above and then do something along the lines of below to determine the value of the parameters.

$segment_array = $this->uri->segment_array();        
$is_location_searched = array_search('location', $segment_array);
if($is_location_searched && $this->uri->segment($is_location_searched +1))
{
    $location = $this->uri->segment($is_sorted+1);
}
Martin Reeves
Missed Toms suggestion of using $this->uri->uri_to_assoc(n) which does seem a cleaner approach than my code above but ultimately the same URL structure.
Martin Reeves
A: 

Have a look at http://lucenebook.com/#/p:solr/s:wiki and click around a bit on the left-hand navigation. Pay close attention to what happens in the url when you do. I really like this scheme for many reasons.

  1. It's SEO-friendly.
  2. "Curious" people can mix/match the urls and it still resolves to a proper search.
  3. It just looks good!

Of course, the trick is really in the code, in how you build the thing. It took me a few weeks to sort it out, but I finally have my own version of that site. Just not ajax based, because I like search engines better than ajax. Ajax don't pay the bills.

jspash