Solved

Creating hierarchal list based on categories table

Posted on 2009-07-14
2
177 Views
Last Modified: 2012-05-07
I have the following category table:

Id     Name              parent_id
-----------------------------------
1      Bakery              NULL
3      Fresh Meats      NULL
4      Bread              1
5      Cake              1
6      Rolls              4
7      Loaves              4
8      Bagettes              4
9      Beef                      3
10      Lamb              3
11      Pork                      3
12    White               7
13    Brown              7

Whats the most efficient way to produce a hierarchal list in SQL/PHP like:

Bakery
> Bread
>> Baguettes
>> Loaves
>>> White
>>> Brown
>> Rolls
> Cake
Fresh Meats
> Beef
> Lamb
> Pork

Also whats the best way to get all the perent categories from the id. So for example if I knew an item was in category id 12 then to get the list:

Bakery
Bread
Loaves
White (this category)
0
Comment
Question by:alex_wareing
  • 2
2 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 24854898
I found a good script for this a while back - I will try to find it and get back to you.
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 24854917
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now