0

Lets assume I have a table like this:

-> id | parent_id | name 
-> 1  | 0         | Food
-> 2  | 1         | Salty
-> 3  | 1         | Sweet
-> 4  | 2         | Patty
-> 5  | 4         | Meat
-> 6  | 3         | Cupcake
-> 7  | 5         | Made on Oven
-> 8  | 5         | Fried
-> 9  | 6         | Chocolate

I wonder what kind of witchcraft i have to make in PHP to get an ordered result dinamically without knowing the ammount of sublevels that the list are going to have like this:

Food
- Salty
- - Patty
- - - Meat
- - - - Made on Oven
- - - - Fried
- Sweet 
- - Cupcake
- - - Chocolate

Thank you!

Phil
  • 157,677
  • 23
  • 242
  • 245
Pablo Cardozo
  • 103
  • 1
  • 10
  • Run a query where `parent_id` equals 0 to get all the top-level categories. Iterate over the results and get all the records with that `parent_id`. Rinse-and-repeat – Phil Sep 22 '17 at 03:59
  • Have you tried something already that isn't working? – Phil Sep 22 '17 at 03:59
  • I can build 2 levels, Main Categories, and one subcategories. But i dont know how to build all the possible subcategories dinamically. – Pablo Cardozo Sep 22 '17 at 04:04
  • Solved with this script: https://stackoverflow.com/questions/29384548/php-how-to-build-tree-structure-list – Pablo Cardozo Sep 22 '17 at 04:07

0 Answers0