Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating hierarchal list based on categories table

Posted on 2009-07-14
2
Medium Priority
?
221 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 35

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 35

Accepted Solution

by:
gr8gonzo earned 2000 total points
ID: 24854917
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

972 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