Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Build a filters system
Old 11-25-2009, 07:24 PM Build a filters system
Junior Talker

Posts: 3
Trades: 0
Hello everyone. This is my first post and is already urging for help...

I'm trying to build a filters system for an online shopping cart. For example, let's say i'm in "Shirts" category. The available filters are: "Country", "Color" and "Team". Each filter has multiple values, for example the "Country" filter has "England", "Belgium", "Austria" values, "Color" filter has "Red","Green","Blue" values and "Team" filter has "Local" and "International" values

More graphical:

[Country]
|____ England
|____ Belgium
|____ Austria

[Color]
|____ Red
|____ Green
|____ Blue

[Team]
|____ Local
|____ International

When i check the "Local" value i'm getting all products that are local.
When i check and the "Green" value, i'm getting all products that are local and green.
When i check and the "England" i'm getting all products that are local, green and ONLY england (the selecting order is random... the country can be selected first and after that the colour and local).

I tried in two ways, both failures.

Option 1.
I have two tables:

t.products and t.filters

[t_products]
idProduct
some_other_field
some_other_field
some_other_field

[t_products] data:
1 | some_data | some_data | some_data

[t_filters_values]
idValue
idProduct
value -- which is type varchar

[t_filters_values] data:

1 | 1 | Red
2 | 1 | Blue
3 | 1 | Local

The query i'm trying is:

Code:
select * from t_products 
inner join t_filters_values on (t_products.idProduct = t_filters_values.idProduct)
where (t_filters_values.value like '%Local%' and t_filters_values.value like '%Red%' and t_filters_values.value like '%Blue%')
and i expect to get one row. But it does not happening. Why ?

If i have only one value to match, it works. For example

Code:
select * from t_products 
inner join t_filters_values on (t_products.idProduct = t_filters_values.idProduct)
where (t_filters_values.value like '%Local%')
Option 2.

I have just one table:

[t_products]
idProduct
filters
some_other_field
some_other_field

[t_products] data:
1 | Red,Blue,Local,Austria | some_data | some_data

If i try

Code:
select * from t_products
where t_products.filters like '%Red%' and t_products.filters like '%Local%' and t_products.filters like '%Austria%'
it works. But if i try

Code:
select * from t_products
where t_products.filters like '%Red%' and t_products.filters like '%Blue%' and t_products.filters like '%Local%' and t_products.filters like '%Austria%'
it fails. Why ?

I thinking that maybe my logic is all wrong. In this case, how should i think the system ? What is the best way to make a filters system ? I'm opened for any tables structure.

The number of filters (and values) is variable and unknown at any given time. A product can have one filter or five or seven or 2 and so on... That's why i did not chose to add extra columns in the product table...

Thank you in advance and thank you for your time spent to read this.
myself2009 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-26-2009, 05:05 AM Re: Build a filters system
Super Talker

Posts: 139
Name: John Davis
Trades: 0
You have to create Product table with each property in separate table columt:
product_id | color | country | team | other...

If product may be multicolor, then you need additioanl table for colors. Then product table will like:
product_id | color_id | country | team | other...
and color table
color_id | color name

Use such rule for other product properties.

To retrive necessary products you have to use JOIN statement to merge number of tables in one query.

In such case WHERE parte will be simple (color_name='red' and country='England' and team='Local')
__________________
»
Please login or register to view this content. Registration is FREE
- Interactive maps for websites
»
Please login or register to view this content. Registration is FREE
for web developers
MapMaster is offline
Reply With Quote
View Public Profile Visit MapMaster's homepage!
 
Old 11-26-2009, 05:46 AM Re: Build a filters system
Junior Talker

Posts: 3
Trades: 0
Yes MapMaster, but what do you do when a product has 20 or more filters ? Let's say a notbook, for example...
myself2009 is offline
Reply With Quote
View Public Profile
 
Old 11-26-2009, 05:57 AM Re: Build a filters system
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Mapmaster is wrong on that one. You need to do the contrary.
Put each type of attribute in a separate table.

You did it right at the beginning of the post
Quote:
[Country]
|____ England
|____ Belgium
|____ Austria

[Color]
|____ Red
|____ Green
|____ Blue

[Team]
|____ Local
|____ International
Then, you need a table that map each attributes to a product:
Code:
table productAttribs:
  productId
  attribId
  attribType
and put 1 line per attribute for each product in that table.

After that, your filter is a simple
Code:
select * 
from products p
  inner join productAttribs a
    on p.productId=a.productId
  left outer join country c
    on c.countryId=a.attribId
    and a.attribType='country'
  left outer join color co
    on co.colorId=a.attribId
    and a.attribType='color'
  left outer join team t
    on a.attribId=t.teamId
    and a.attribType='team'
-- Up this mark, is the "base" of the query. Under this mark is the filtering 
where p.productId={your product id}
and co.value={your color value}
and c.value={your country}
.....
You will have to include each attribute tables in the "outer joins" in the base query, and add the where clause elements as far as you have them.

You may need to fiddle a bit with this, but I think this is the base of your solution.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 11-26-2009 at 06:00 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-26-2009, 06:43 AM Re: Build a filters system
Junior Talker

Posts: 3
Trades: 0
Thank you for your answer tripy, but in your way isn't that means that i have to create a separate table for each filter ? For a notebook, for example, i will have 20 or more tables... What happens when a new filter shows up ? I create an extra table for it ?
myself2009 is offline
Reply With Quote
View Public Profile
 
Old 11-26-2009, 07:04 AM Re: Build a filters system
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
That's the idea, yes.

Or, but it's usually not recommended, put every attributes in one table, with a value and a group.
It's not recommended mostly for readability and ease of extention , but if you want to go that way, I'd make a table like this:
Code:
table attrib:
  attribId
  productId
  group varchar(50)
  intVal integer
  numVal float
  strVal varchar(100)
  bolVal boolean
and for each group, put the value in the field that correspond to your data type.
then the query shoudl be
Code:
select * 
from products p
  inner join attribs a
    on a.productId=p.productId
where p.productId={your product id}
and a.group='color' and a.strVal='green'
and a.group='size' and a.intVal=38
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Build a filters system
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23704 seconds with 12 queries