http://qs321.pair.com?node_id=608298

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

Make this the fourth SOPW on this problem, hoping this will be the last :) In short, I have a MySQL database that has infinite levels of categories/subcategories. It's set up like..
id parentid catname catdesk
Using the parentid to locate the next-up category to which it belongs. If it's 0, it's a parent category. I wrote the hack below but it seemingly goes through a loop and reprints the subcategories many times.
# print the beginning of the select print "<select name='categories' SIZE='5'>\n"; # select top-level elements, and specify top level recurQuery(0,0); # my super recurse the tree function, takes 2 options being parentid, +and current level sub recurQuery { my $parentId=$_[0]; my $level=$_[1]; my $data = "SELECT id, parentid, catname from categories WHERE + parentid = $parentId"; my $sth = $dbh->prepare($data); $sth->execute() or die $dbh->errstr; # loop through results while (my @results = $sth->fetchrow_array) { # print the beginning of the option box print"<option name='$results[2]'>"; # loop based on level, for indentation for (my $i = 0; $i < $level; $i++) { print "&nbsp;*"; } # print end of option box print $results[2]."</option>\n"; # find child elements of this one my $data = "SELECT id, parentid, catname from categori +es WHERE parentid = $results[0]"; my $sth = $dbh->prepare($data); $sth->execute() or $dbh->errstr; # get number of rows returned my $rownumbersub = $sth->rows; # if rows returned, exec this if($rownumbersub > 0) { # loop through children while (my @resultssub = $sth->fetchrow_array) +{ # re-exec this function with the child + ID and new level recurQuery($resultssub[1],$level+1); } } } } # close that thar select box
The results are as follows. As you can see, the subcategories reprint themselves. They are only in the database ONE time, so that can't be the problem. Can anyone see what might be going on her?
<select name='categories' SIZE='5'> <option name='insect'>insect</option> <option name='mammal'>mammal</option> <option name='Sea Life'>&nbsp;*Sea Life</option> <option name='Dolphin'>&nbsp;*&nbsp;*Dolphin</option> <option name='Seal'>&nbsp;*&nbsp;*Seal</option> <option name='Dolphin'>&nbsp;*&nbsp;*Dolphin</option> <option name='Seal'>&nbsp;*&nbsp;*Seal</option> <option name='Cats'>&nbsp;*Cats</option> <option name='Dogs'>&nbsp;*Dogs</option> <option name='Sea Life'>&nbsp;*Sea Life</option> <option name='Dolphin'>&nbsp;*&nbsp;*Dolphin</option> <option name='Seal'>&nbsp;*&nbsp;*Seal</option> <option name='Dolphin'>&nbsp;*&nbsp;*Dolphin</option> <option name='Seal'>&nbsp;*&nbsp;*Seal</option> <option name='Cats'>&nbsp;*Cats</option> <option name='Dogs'>&nbsp;*Dogs</option> <option name='Sea Life'>&nbsp;*Sea Life</option> <option name='Dolphin'>&nbsp;*&nbsp;*Dolphin</option> <option name='Seal'>&nbsp;*&nbsp;*Seal</option> <option name='Dolphin'>&nbsp;*&nbsp;*Dolphin</option> <option name='Seal'>&nbsp;*&nbsp;*Seal</option> <option name='Cats'>&nbsp;*Cats</option> <option name='Dogs'>&nbsp;*Dogs</option> <option name='bird'>bird</option> </select>