Jump to content

Mysql PHP Two Table Tree or Nested Results


dprichard

Recommended Posts

Okay, I have been putting off trying to figure out how to do this and I am at a crossroad where I just gotta figure it out.

 

I have the following

 

Table 1

 

NewsCategoryId

Category Name

 

Table 2

 

NewsStoryId

NewsCategoryId

 

I want to be able to show it on my PHP page page like this:

 

News Category1

- Story 1

- Story 2

- Story 3

 

News Category2

- Story 1

- Story 2

- Story 3

 

 

I have been searching google, but am not sure what I am trying to do is called so I am not getting good results.  Any help or direction would be greatly appreciated.

 

I know I can do the following, but I can't imagine doing a query for each result from table 1 is good practice and will probably be a load on the server:

 

Query 1

 

While $row = mysql fetch array

 

Query 2

 

But I am sure there is a better way.

Link to comment
Share on other sites

Sorry, I guess more like this

 

TableNewsCategories

- CategoryId

- CategoryName

 

 

TableStories

- StoryId

- StoryName

- CategoryId

 

 

Then in my page like a tree I want to echo out

 

CategoryName1

- Stories under that category

 

CategoryName2

- Stories under that category

 

Just not sure what doing this is called so I dont know how to query it in google to figure out how to do it.

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.