Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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>

In reply to Possible loop problems in database query by coldfingertips

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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 or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-01-21 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (59 votes). Check out past polls.

    Notices?