When inserting products in an eshop we often need to link some products (aka related products) to others and the linking must be done both ways, meaning if I link product1 to product2 then product2 must also be linked to product1.
Which is the best practice, using an extra table ‘relations’ (prodid, related_prodid) or to keep a list of related products in a delimited string in each product’s row in the products table?
In either case, we would also need a recursive method to loop through a given array of products and insert/update the tables with the relations, could someone help me out with the algorithm? I will do the PHP coding but I cant think of a good way to do it.
You’d better use an intermediate table
related_to(id, product1, product2)Then, you’ll use the code:
Of course, you need to
JOINthis table with your product table, but since I don’t have much informations about your mysql structure, I’ll let you check on this site if you don’t know how.