Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I have a table of categories in the following structure: id, name, isin

'isin' is the id of a category that this category is inside. The others should be self-explanatory.

What's the best way of sorting and writing this to the screen in a nice tree diagram?

EG. Something like:
/ /cat1 /cat2 /cat2/cat3 /cat2/cat4 /cat5 /cat5/cat6 /cat5/cat6/cat7 ...and so on...
Regards,
Allen

Replies are listed 'Best First'.
Re: Perl/MySQL: Working with tree of categories
by Krambambuli (Curate) on May 06, 2007 at 10:24 UTC
    Unless you've already done that, it's definitely worth looking around the monastery searching for 'adjacency lists' and 'trees'. You might discover that there are many ways to tackle the problem - and that, where you thought having _one_ problem to solve, you're suddenly eager to solve _a bunch_ of problems that you ignored so far :)

    That said, might be that DBIx::Tree is exactly what you're after - so far ;)

Re: Perl/MySQL: Working with tree of categories
by monkfan (Curate) on May 06, 2007 at 07:43 UTC
Re: Perl/MySQL: Working with tree of categories
by GrandFather (Saint) on May 06, 2007 at 06:40 UTC

    Terminal mode, CGI, GUI, something else? What is the context? How many entries will there be? How deeply nested? Is the user expecting to navigate the tree and perhaps collapse branches?

    "Best" is meaningless almost always - it depends on context and often on personal preference. At the very least give us the context.


    DWIM is Perl's answer to Gödel
Re: Perl/MySQL: Working with tree of categories
by Anonymous Monk on May 06, 2007 at 06:40 UTC
    That sounds ok. This also
    +---cat1 \---cat2 +---cat1 +---cat2 | +---cat1 | \---cat2 | \---cat1 | \---cat1 +---cat3 +---cat4 +---cat5 \---cat6 \---cat1 \---cat1 \---cat2
Re: Perl/MySQL: Working with tree of categories
by Anonymous Monk on May 06, 2007 at 16:49 UTC
    Thank you all so much.

    Much great info from other boards. This site does indeed hold up to its reputation.

    Thanks all,
    Allen