-3

in Mysql what is the best Method to save a list of colors available for a product? there are alot of products and every product may have more than two color (ex:fabric color1,fabric color2,wood color).

Omar Sedki
  • 608
  • 6
  • 14

3 Answers3

2

I would go with Product table, Colors table and then table to connect them (id_product, id_color). This was any product can have more than one color, and you can easily find all product with selected color. You can make similar for other attribute.

But you might want to have separate table for product attributes (similarly to color)

Volvox
  • 1,639
  • 1
  • 12
  • 10
-1

There many ways you can do that. here are my ways 1:make a colour table with foreign key of product 2:if you want a single table then save colours with comma separated or some special character(like | ) with each product

Ahsan aslam
  • 1,149
  • 2
  • 16
  • 35
  • Option 1 introduces redundancy in the colour table. Option 2 defeats the very purpose of a RDBMS. See: [Is storing a delimited list in a database column really that bad?](http://stackoverflow.com/q/3653462/1446005). – RandomSeed Mar 20 '15 at 07:54
-2

You can use html5 colorpicker for inputs or manually input the colors code/name etc and have multiple input fields per product get them as an array while saving in database you should have a color column in your products table and then use php's implode function to make the array as string separated by commas and save the string in the color column. While fetching the data you should use php's explode function to get the respective colors in the respective fields of a specific product.

Irshad Ali Jan
  • 203
  • 1
  • 10