id parentid catname catdesk #### # print the beginning of the select print "\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""; # loop based on level, for indentation for (my $i = 0; $i < $level; $i++) { print " *"; } # print end of option box print $results[2]."\n"; # find child elements of this one my $data = "SELECT id, parentid, catname from categories 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 #### insect mammal *Sea Life * *Dolphin * *Seal * *Dolphin * *Seal *Cats *Dogs *Sea Life * *Dolphin * *Seal * *Dolphin * *Seal *Cats *Dogs *Sea Life * *Dolphin * *Seal * *Dolphin * *Seal *Cats *Dogs bird
## # print the beginning of the select print "\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""; # loop based on level, for indentation for (my $i = 0; $i < $level; $i++) { print " *"; } # print end of option box print $results[2]."\n"; # find child elements of this one my $data = "SELECT id, parentid, catname from categories 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 ##
## insect mammal *Sea Life * *Dolphin * *Seal * *Dolphin * *Seal *Cats *Dogs *Sea Life * *Dolphin * *Seal * *Dolphin * *Seal *Cats *Dogs *Sea Life * *Dolphin * *Seal * *Dolphin * *Seal *Cats *Dogs bird