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
Solved

Creating hierarchal list based on categories table

Posted on 2009-07-14
2
203 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

809 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