?
Solved

Creating hierarchal list based on categories table

Posted on 2009-07-14
2
Medium Priority
?
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to implement server side field validation and display customized error messages to the client.
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

770 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