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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.