Question

What is the best practice to do a searching form with filters, where the filters are depending on the category? e.g.:If you go to visit the ebay and select a category then the filters are different on the left hand side in the cell phones (filters: brand, operating system...) or the fashion category (filters: size, color...)...

In my mind I would do more table in DB. Each table for one category (cat_cellphone, cat_fashion...). Then put a product one of these tables depending on the category (not one products table where one column contains the category ID). These tables are different where the columns names are characterized by the category. Next, should do more searching forms and call a form where the filter belongs to the category.

Is it a good concept or there is an other accepted practice in big projects?

Was it helpful?

Solution 2

Using multiple tables to store your products isn't a good idea. Because there will be products that overlap in categories which would result storing the same product in more than one table.

Just use a product table with the ID, product_number, description, etc. and a category table to store the different categories. Then you could link them directly like:

Product table:

ID product_number description category
1  00001          Screwdriver 1

Category table: ID description 1 Tools

And you could even expand the category table with an extra column to use subcategories by addressing the parent of the subcategory:

Category table with subcategories:

ID description      parent
1  Tools            NULL
2  Automatic tools  1

And if you don't like directly linking to the category table from the product table you could use a link table:

Product_category:

Product_ID Category_ID
1          1

Hope this answers your question.

Edit, added filter table:

To add filtering for a product you can use a table for a the filters and a link table, like so:

Filter:

Filter_ID description value
1         brand       Bosch
2         brand       Bahco
3         type        Phillips

Product_Filter:

Product_ID Filter_ID
1          1
1          3

That way you can link more than one filter to each product and use the same filter more than once.

You could even expand this further by using another table for the filter values, but that could make things a bit to complicated:

Filter:

Filter_ID description value
1         brand       1

Filter_value:

Filter_ID value
1         Bosch
2         Bahco

OTHER TIPS

No, having multiple tables is a bad idea.

In general. use a table with a primary key over two columns instead. (The primary key may span more than one column.)

The columns would be categoryname / filtername.

If you don't like such primary keys, and always use an autoincrement column, you can still create an index over the two columns.

The columns would be: id / categoryname / filtername / filtertext

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top