Hello all...I'm looking for some suggestions on a good method for creating nested categories. What I would like to do is store the category info in a database with 3 columns:

---------------------------------------------------------
| category_id | parent_id | category_name |
---------------------------------------------------------

That's easy enough...where I'm having trouble is retrieving this data in a way that makes it easy to place into a drop down list on a web page like this:

Category 1
 - Subcategory 1
   - Subsubcategory1
     - Subsubsubcategory1
     - Subsubsubcategory2
 - Subcategory 2
   - Subsubcategory1
   - Subsubcategory2
   - Subsubcategory3

This is what I have so far (not the entire script...just the relevant parts):

my ($category_list) = getCategories(); ### Trying to figure out how to loop through $category_list for (my $search_total=0; $search_total<=$#$category_list; $search_tota +l++) { # Do something with these: # $category_list->[$search_total]->{category_id} # $category_list->[$search_total]->{parent_id} # $category_list->[$search_total]->{category_name} } ### Used to do a single select to grab all categories sub getCategories { my ($results_data) = []; my $row = 0; my $sql = qq~SELECT category_id, parent_id, category_name FROM +ss_catalog_categories~; my $sth = $dbh->prepare($sql); $sth->execute; while (my @results = $sth->fetchrow_array()) { $results_data->[$row]->{category_id} = $results[0]; $results_data->[$row]->{parent_id} = $results[1]; $results_data->[$row++]->{category_name} = $results[2]; } $sth->finish; return ($results_data); }

Any suggestions on doing this correctly would be wonderful. Thanks.


In reply to Nested Categories by spaceout

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.