How to create a product relationship with php and Mysql

Asked

Viewed 259 times

1

Some time ago I made an attempt to relate products, but it did not become functional and I believe it was also not the most correct form, I confess, the same is bad, what I did was basically this:

I search the lines (Categories) and just below where the checks list the products of this category and select the product I want to relate, this can be seen in this image:

inserir a descrição da imagem aqui

My cheks are set up like this:

<input type="checkbox" name="produto[]" id="produto[]" value="<?php echo $row_produtos['id_produto']; ?>" /><?php echo $row_produtos['codigo']; ?>

I’m limiting the amount of 10 products, I’m confused and I can’t understand what I should record in my bank, the id_product, id_categoria to later search and show on my page.

What I’d like to know is, how is the right way to develop this solution and visual, how would the register?

  • A product can have N categories that’s it?

  • Hello @rray, that’s right.

  • I had a similar doubt, see if this resolves http://stackoverflow.com/q/23160254/1518921 (the idea of the question is a product with several prices, should help you), of course I used double at the time for being totally lay, but this has nothing to do with the problem hehehehehe, Just ignore, otherwise you might want to read this: http://answall.com/a/6448/3635, of course, if you want to show the product you won’t always need to do the relationship

  • You will need a table for products, categories and a call possibly produto_categoria who has the id of the product and the idof the category at least.

  • Yes, @array, these tables I already have, including with the names you have.

  • The question is when to use PHP to record? the product id comes from form with vc can take the category ids and do a foreach to register in the table of produto_categoria

  • The category ids I believe, should stay in my checks, ai caught them by foreach, my crucial doubt was what to record in the bank.

  • That’s right, the product id should be an Hidden input

Show 3 more comments

1 answer

1


A suggestion:

First, if I’m not mistaken, you shouldn’t make an HTML ID array like this: id="product[]" (leave only the name)

Now: In the database part, simply create an intermediate table to relate the products between themselves, or else, you would have two foreign keys in this table pointing to the same primary key of your products.

In the visual part. You could categorize this kind of thing in editing the product itself, not in its category.

On the product screen, you can list all other products in the same category and thus, you select which ones are related. This would be saved with N (where N can be up to 10 by its rule) rows in this new table where the first key is the product itself, and the second key is the related product.

Just remember that you would need to always search both keys, since the product itself may be on the first and/or the second.

  • Hello @Mario from M. Barros Neto, your tips were very important for the solution of my problem, thank you.

Browser other questions tagged

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