Welcome Guest, Not a member yet? Register   Sign In
Lots of paramters in the url (How would you do this)
#1

[eluser]tomdelonge[/eluser]
I'm making something similar to a shopping app. In the past, if I've needed to use the urls to generate queries, it was usually pretty simple, like:

example.com/product/view/by_brand/BRANDIDHERE

However, this store requires a lot more complication than that. I want to have an action in my controller that basically will show the items according to the parameters. But how would you do complicated ones, like

-All items with Brand X OR Brand Y
-And all items with color A and color B

You know what I mean? I don't have any problem at all figuring out what queries to do, that's easy.

However, I want to design the urls in the best way possible. How should I take them apart? I understand the uri class just fine, But I run into problems like this:

If I go to an associative array, and need to be able to do two brands, how could you do that? (And I want an unspecific amount of brands possible, not just to have a "brand_1" and "brand_2" paramter.

Also, how could I use "And" (like my earlier color example)

Suggestions? Just give me some ideas please.
#2

[eluser]pickupman[/eluser]
You've got the right idea. Try example.com/product/view/by_brand/1_2_3 . Then you would use
Code:
$brands = $this->uri->segment(4,FALSE);

if($brands){
  $brands = explode("_",$brands);

  foreach($brands as $id){
    $this->db->or_where('brand_id',$id);
  }
}
This should work fine if you have something like checkboxes for users to select the brands they are looking for. Post the form to the controller to create your redirect links.
Code:
if($this->form_validation->run()){
  $brands = $this->input->post('brand_id');
  $segment = '';
  foreach($brands as $id){
   $segment .= $id.'_'; //Add each brand id to uri segment
  }
  $segment = rtrim('_',$segment); //Strip last _ at end of string

  redirect('example.com/product/view/by_brand/'.$segment); //Send user to uri with parameters
}

If you were wanting to go the SEO url route, you could use brand names instead, and just lookup the names in the DB to convert them to ids.
#3

[eluser]n0xie[/eluser]
Quote:-All items with Brand X OR Brand Y
-And all items with color A and color B
Sounds like what you are trying to do is showing a view based on filters. This is usually done when 'searching' for certain items. Search is easier when done using GET parameters.
Code:
http://domain.tld/product/search?brand=X&brand=Y&color=A&color=B
#4

[eluser]adamp1[/eluser]
Or you could use the following:

This will store an array into the URI:
Code:
/Turns
$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');
$str = $this->uri->assoc_to_uri($array);

// Into
product/shoes/size/large/color/red

Then for the reverse use to get the array out of the URI.
Code:
$this->uri->uri_to_assoc(n);
#5

[eluser]Jondolar[/eluser]
Since these types of page requests are typically not done by search engines, but by specialty searches by users (so you don't have to worry about getting the pages spidered), you don't really need to try to do this via the url. Unless your interface is quite strange, you probably should either allow the selection of one "setting" at a time or use a form. In either case, you only need to capture the selected data once and store it in a session/cookie until the special settings are removed. When you offer up the next screen, you don't need to continue your url to build upon the previous selections since they are stored in a session. Therefore, either pass one setting at a time or use a form.

Based on your example of having two settings for "brand", I can't see how you would normally build a url link with every combination of brands (unless you only had one or two). Therefore, your interface must either be asking for brands to be added to the search one at a time (via link) or via a post. If you offer additional settings after the initial post, don't try to pass the previous selections, store them in a session and build upon them.

These are just suggestions as your program may make sense to do it another way but this is how I have done it.
#6

[eluser]pickupman[/eluser]
I think using a session can be tidier, as it also allows you to do pagination easier.
#7

[eluser]deadelvis[/eluser]
Hi guys,
I am very curious about what's been discussed in this post as I am struggling with the same problem of needing several url parameters/filters, while still trying to keep code simple.

While sessions does indeed make things simpler and tidier.... how would you handle that combined with page caching??
When a cached page is served, and since it relies on how unique is the url, aren't the session variables ignored, and the same page shown to everyone independent of the session vars?

Sad




Theme © iAndrew 2016 - Forum software by © MyBB