Matt
Matt

Reputation: 9433

How can I have Code Igniter URI segments for multiple variables?

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.

Upvotes: 0

Views: 6963

Answers (4)

jspash
jspash

Reputation: 191

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.

Upvotes: 0

Martin Reeves
Martin Reeves

Reputation: 333

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);
}

Upvotes: 0

Tom Schlick
Tom Schlick

Reputation: 2318

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

Upvotes: 2

Shane
Shane

Reputation: 16827

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.

Upvotes: 0

Related Questions