Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Possible loop problems in database query

by pgor (Beadle)
on Apr 04, 2007 at 16:56 UTC ( [id://608325]=note: print w/replies, xml ) Need Help??


in reply to Possible loop problems in database query

The intent of your recursive function is "find every immediate child of the specified parentId, print it as an option element, immediately followed by all of its children, etc." There are two issues with your code breaking this:
  1. Your method is handling two levels per invocation, instead of just one.
  2. The recursive call to your function is passing the current element's parentid, not the id. (Remember, "immediate child of the specified/current element.")
This should help:
# my super recurse the tree function, takes 2 options being parentid, +and current level sub recurQuery { my ($myParentId,$level) = @_; my $data = "SELECT id, parentid, catname from categories WHERE par +entid = ?"; my $sth = $dbh->prepare($data); $sth->execute($myParentId) or die $dbh->errstr; my $indent = '&nbsp;*' x $level; # loop through results while (my ($id,$parentid,$catname) = $sth->fetchrow_array()) { # print the beginning of the option box print"<option name='$catname'>$indent$catname</option>\n"; recurQuery( $id, $level + 1 ); } }
pg

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://608325]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-23 07:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found