1

On our online shop (PHP/MySQL), for our category structure we are using a transitive closure table (ancestor, descendant, length) as described by Bill Karwin.

I am finding it very flexible and useful, but can not work out how to print the complete category tree without recursion. Is this possible?

For example, I want an output similar to:

  • Audio
    • Portable
      • MP3
  • Computing
    • Optical Drives
      • DVD-RW
    • Input Devices
      • Wireless
        • Keyboards
  • Household Appliances
  • Televisions
    • LCD
      • Widescreen
    • CRT

Optionally limiting by the total category depth.

Is this a limitation of this model, or is there a way around it? At present I have been using recursion and caching the result.

Paul
  • 73
  • 6

1 Answers1

2

Recursion is the simplest way to do it - especially if it's for display.

Alternatively you can do it from MySQL along the lings of the advice in this answer How can I find all siblings to my node and its anchestors in a hierarchical category tree?

Community
  • 1
  • 1
Brian
  • 8,418
  • 2
  • 25
  • 32
  • I assume you have written a function to do it - if not I can supply one I use for exactly this purpose. – Brian Jun 30 '11 at 12:01