Web Analytics Made Easy -
StatCounter Need help understanding threaded comments code - CodingForum

Announcement

Collapse
No announcement yet.

Need help understanding threaded comments code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help understanding threaded comments code

    Hey. Basically I am porting a PHP script to coldfusion (with minor mods) however the threading aspect is something I still dont fully understand. (I am trying to make a threaded commentary system.) The code is pasted below for the function. I understand most of it except mainly how $level and $comment work, and although i know its a while loop and the function keeps calling itself I dont understand however how its stopping itself and how the end ( if ($level && $comments) ) works. Could somebody please explain these two things for me?

    Much Appreciated.

    PHP Function:
    PHP Code:
    <?

    function DisplayKids ($topic_id=0$level=0)
    {
        
    $comments 0;
        
    $query "select topic_id, name, author "
            
    .", create_dt, description from px_topics where "
            
    ." parent_id = $topic_id "
            
    ." order by create_dt, topic_id "
        
    ;
    #print "<p>$query</p>\n";
            
    $result mysql_query($query);
        while (list(
    $r_topic_id$r_name$r_author$r_create_dt$r_description)
            = 
    mysql_fetch_row($result)
        )
        {
            if (!
    $level)
            {
                if (!
    $comments)
                {
                    print 
    "<b>Comments:</b><br>\n";
                }
                print 
    "<table border=0>\n"
                
    ."<tr bgcolor=skyblue><td colspan=2 width=500>\n";
            }
            else
            {
                if (!
    $comments)
                {
                    print 
    "<ul>\n";
                }
                print 
    "<li>";
            }
            
    $comments++;
            if (!
    eregi("[a-z0-9]",$r_author)) { $r_author "[no name]"; }
            if (
    $r_topic_id != $topic_id)
            {
    ?>
    <a href="display_topic.phtml?topic_id=<? print $r_topic_id?>"><? print $r_name?></a> by <b><? print $r_author?></b>
    <?
            
    }
            else
            {
    ?>
    <? 
    print $r_name?> by <b><? print $r_author?></b>
    <?
                       
    }  
    ?>
    <? print $r_topic_id?> )
    <?
            
    if ($level)
            {
                print 
    "<br>\n";
            }
            else
            {
                print 
    "</td></tr>\n"
                    
    ."<tr><td width=2> </td>\n"
                    
    ."<td width=498>$r_description</td>\n"
                    
    ."</tr></table><br>\n"
                
    ;
            }
            
    DisplayKids($r_topic_id$level+1);
        }
        if (
    $level && $comments)
        {
            print 
    "</ul>\n";
        }
    }
    ?>

  • #2
    Originally posted by nycitservices
    I understand most of it except mainly how $level and $comment work, and although i know its a while loop and the function keeps calling itself I dont understand however how its stopping itself and how the end ( if ($level && $comments) ) works. Could somebody please explain these two things for me?
    If the call to MySQL returns results, the while loop will be entered (for each row returned), at the end of which, the function will be called with the current value of $r_topic_id. When there are no more rows (or the query returns none to begin with) the while loop is bypassed and and the function returns to the function that called it (which could be itself with a different $r_topic_id). Basically it burrows done each thread until the end and then works its way back up. Its a recursive function.

    The if ($level && $comments) section just closes the HTML elements if they've been opened (though I can't tell just by looking at it if it works).
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

    Comment

    Working...
    X