Add the same product in various categories

Asked

Viewed 154 times

0

Good

And the following I have a table categories and another table products at this time when creating the product I select the category to which it belongs by selectbox only now I have the need to associate a products to various categories I would like to know what may be the best way to associate the same product to several categories.

CREATE TABLE IF NOT EXISTS `estabelecimentos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_mae` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`titulo` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`link_facebook` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`link_mapa` text COLLATE utf8_unicode_ci NOT NULL,
`distritos` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`concelhos` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`morada` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`contacto` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`int_preco` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`link_site` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`introducao` text COLLATE utf8_unicode_ci NOT NULL,
`servicos` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`descricao` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`keywords` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`keywords_pesquisa` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`google_verification` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`activo` tinyint(1) NOT NULL,
`pos` bigint(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=198 ;
  • databases is Myisam

  • It is possible to do with MyISAM also, but would be linked to a "Model" in PHP. In more relational databases (as innoDB) you would use the 1:N (one-to-Many), but could also use Many-to-Many something like: https://netbeans.org/images_www/articles/73/javaee/ecommerce/data-model/many-to-many.png

  • Then it will be better to convert the database to innoDB and better ?

  • Ok so how could I put a php model keeping the same structure I have in the above database

  • I already have backoffice and front-end at this time I do the Insert but only for a category through selectbox wanted and implement for more than one category

  • Yes César, I understand, I really wanted to tell you that you would need a table with the categories and one with the products, the back-end (which is different from backoffice) and the front-end (which is html, javascript) would be examples of how to do the process.

  • Can you help me create that I really need to make that system

  • Good evening Caesar, it really is a system that takes a certain amount of time to develop and still formulate this as a good answer. It’s not that I don’t want to, it’s that right now I won’t be able to do something really functional in a little while. But I promise to see if I can buy some time and if I’m really capable of creating something useful :)

  • One solution I was thinking of was to insert an array with the categories in the article column

Show 4 more comments

1 answer

1

A solution is to use a weak entity for this, assuming the model

categorias
----------
categoria_id
categoria_nome

produtos
---------
produtos_id
produtos_nome

categorias_produtos
--------------------
categorias_produtos_id
produto_id
categoria_id

Which would be

categorias 1 -------- * categorias_produtos * ---------- 1 produtos
  • My product structure put up on the topic on which the field id_mae and where it takes the name of the category currently this so wanted a way not to have to change the structure too much

  • Adir’s response and the good answer. What you have as a problem is common: products, suppliers plus a supplier can sell but of a product and vc can buy the same product in various suppliers. The solution is to have a third table that links between supplier and products (and vice versa). Your problem is that you made the table before thinking about it. Now, unfortunately, I think you need to change the structure.

  • But this way I insert the categoria_id in an array seperated by commas ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.