Solved

Creating hierarchal list based on categories table

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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 is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

772 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