Jump to content

Associate Table Help!


wrigley

Recommended Posts

Ok, please try to help as much as you can, thank you very much!

 

I have 3 tables (kb_category, kb_products, kb_category_products) the kb_category of course holds all the categories including a category_id.  kb_products of course holds all the products including a product_id and category_id. kb_category_products which simple has category_id and product_id.

 

What I need is to have only the products that are in that category be displayed.  So the kb_category_products is to handle exactly that.  I can get the page to realize that kb_category.category_id=3 (or whatever url variable), but how do I get it to read all the category_id=3 from kb_category_products and to output all the product_id's that are listed in kb_category_products.  And then to get those product_id's from kb_products to output all the correct product information.

 

Please let me know if this doesn't make since, I know it's alot and I probaly didn't explain it very well..lol.

 

Again please help me the best you can.

 

Thank You.

Link to comment
https://forums.phpfreaks.com/topic/167065-associate-table-help/
Share on other sites

Do the products have multiple categories? If not, then you may be better off dropping the joining table and just store the category_id as part of the product itself.... but I digress...

 

I am guessing at the field names, and it could probably be done a little simpler with a JOIN in there... but this is the way I would do it (if I understand you correctly).

 

I formatted it goofy, to make it a little more readable.

 

SELECT 
  kb_products.product_name, 
  kb_products.product_description, 
  kb_products.product_price, 
  kb_category.category_name 
FROM 
  kb_category, 
  kb_products, 
  kb_category_products 
WHERE 
  kb_category_products.product_id = kb_products.product_id 
&& 
  kb_category_id = '$id'

 

This is obviously not a copy and paste code block.... you will need to modify it for your particular needs, but I think it should give you a good idea how to extract only the information you want.

 

Nate

Link to comment
https://forums.phpfreaks.com/topic/167065-associate-table-help/#findComment-880972
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.