According to your table, "Sharks" has the sub-categories "Small Critters" and "Sharks"(!). That doesn't make sense. Both should probably be subcategories of "Salt Water".

The typical representation in Perl of a tree like yours would be a hash of arrays. Keys are the category names and values hold the list of corresponding sub-categories. Leaving all database issues aside, I'll assume (a corrected version of) your table is presented as a text file. The following code builds the tree hash %tree from the data. The sub print_tree then prints an indented table of the categories.

my @names; my %tree; while ( <DATA> ) { chomp; my ( $id, $parent, $name) = (split ' ', $_, 5)[ 2, 3, 4]; $names[ $id] = $name; if ( $parent ) { push @{ $tree{ $names[ $parent]} }, $name; } } print_tree( 'Animals', \ %tree, 0); exit; use constant INDENT => ' '; sub print_tree { my ( $root, $tree, $level) = @_; print INDENT x $level, $root, "\n"; for ( @{ $tree->{ $root} } ) { print_tree( $_, $tree, $level + 1); } } __DATA__ Edit Delete 1 0 Animals Edit Delete 2 1 Mammals Edit Delete 3 1 Fish Edit Delete 4 2 Dog Edit Delete 5 2 Cat Edit Delete 6 3 Fresh Water Edit Delete 7 3 Salt Water Edit Delete 8 7 Sharks Edit Delete 9 7 Small Critters
Anno

In reply to Re: Database categories by Anno
in thread Database categories by coldfingertips

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.