Jump to content

Recommended Posts

Hello,

 

Hopefuly someone here will be able to help me.

I'm trying to produce a PHP string from an SQL query. I have a category table and want to make an array or string that has all the child categories in it so that my menu can open the ID numbers upto the desired category

 

My table is like this (Sample Data)

 

Name: stockcats

 

ID |ParentID | Name

-----------------------------

1 |0 | Office Items

2 |0 | Electrical Supplies

3 |1 | Paper

4 |3 | Copier Paper

5 |3 | Photo Paper

6 |5 | Glossy Finish

 

 

So if i ask for the childs to "Glossy Finish" then i would like the query to return the rows with IDs: 6,5,3,1

 

I hope i've made this clear enough. I don't want the client to be restricted by the amount of category levels.

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/166052-advanced-mysql-query-query/
Share on other sites

The following article helped me a lot with that exact issue you're having

http://www.sitepoint.com/article/hierarchical-data-database/

 

The easiest way would be by using a recursive function although it would be inefficient since it would be using a bunch queries.

You probably should go with the Tree Traversal option.

 

Personally I use doctrine framework and the NestedSet feature to handle hierarchical data for me but that's only if you're interested in using ORM.

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.