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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|