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:
- Your method is handling two levels per invocation, instead of just one.
- 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 = ' *' 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
-
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.
|