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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.