Greetings, monks!

I finally decided to implement a "comments" section/feature for my (future) web site, and the basic flat layout (like on fark.com) was ready in a matter of hours. The next step was implementing the threaded model, like on pm or slashdot.

My comments table in the DB (Postgre) has a comment id (cid) and parent id (pid) columns. This simple method provides enough information to figure out the relationship, but I ran into problems when trying to output the comments.

My first idea was, for each comment, to query the DB for comments where their pid == this comment's cid. Add that comment, repeat. When there were no more such comments, drop to the previous level, and continue. Besides not being able to make this work properly, this approach doesn't look right to me, especially from the DB queries point of view.

After a while I came to the conclusion that I'll need to build a tree and then add the comments when traversing it. After some more time with the pm DAG_Node tutorial and experimenting with references, I managed to get this test script to work:

my $comm_tree = Tree::DAG_Node->new; #create the root node $comm_tree->name('CommentRoot'); my %noderef; #hash of node references, with PID as keys $noderef{0} = \$comm_tree; #replies to the story have PID==0
while ( my $hashref = $query->fetchrow_hashref() ) { my $tmpref = $noderef{ $$hashref{pid} }; #reference to the parent node my $cid = $$hashref{cid}; #CID will be used several times my $nodeone = Tree::DAG_Node->new( { name => $cid, #CID as node name mother => $$tmpref, #mother is the parent } ); $noderef{ $cid } = \$nodeone; #add this node ref to the hash $comm_hashref{ $cid } = $hashref; }
$comm_tree->walk_down( { callback => sub { my $node = shift; my $tmp = $comm_hashref{ $node->name }; $$tmp{level} = $_[0]->{_depth}; #add a level key for ht +ml indentation push @comm_loop, $tmp unless ($node->name eq 'CommentRoot' +); #for the HTML::Template loop }, _depth => 0, treename => 'CommentRoot' } ); $template->param(comment_loop => \@comm_loop);
I was on the verge of posting this question yesterday (before getting the tree method to work) but it still might be (conceptually) wrong, and/or poorly coded. So, is this a reasonable approach? Are there any performance issues? Any comments are appreciated.

In reply to Implementing threaded/nested forum by mobby_6kl

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.