Jump to content

I need help with a nested foreach loop


Presto-X

Recommended Posts

Hello everyone,

 

In the past I have done one query and loop the results then did another query within that loop, I'm guessing this is not the best way to do this, I'm guessing there is a way to do one query very mySQL and then loop all of the parent and child rows.  So this is what I'm trying to do, I have 3 tables "categories", "courses", and "classes", categories is the parent to courses and courses is the parent to classes, I'm trying to loop the content of each of the tables out on the page.

 

ie. something like this:

 

Category 0
Course 0
  Class 0
  Class 1
  Class 2
  Class 3
Course 1
  Class 0
  Class 1
Course 2
  Class 0
  Class 1
  Class 2

If anyone could point me in the right direction for the correct term to be searching for I'm sure I could find the answer, but right now I'm stumped, I have played around with JOIN and UNION and that did not end up giving the results I was looking for, it just added all of the data in to each of the rows and looped them out.

Link to comment
https://forums.phpfreaks.com/topic/245951-i-need-help-with-a-nested-foreach-loop/
Share on other sites

really depends on how you tables are set up.. but from first glance here I would suggest looking further into using a JOIN, depending on whether or not you have like fields.. you can use a LEFT JOIN or INNER JOIN most likely

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.