Page 1 of 1

Need help understanding threaded comments code

Posted: Tue Aug 02, 2005 10:17 am
by nycitservices
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. Basically 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:

Code: Select all

<?
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";
    }
}

?>

Posted: Tue Aug 02, 2005 12:22 pm
by josh
if ($level && $comments)

That means if $level can be evaluated as true, and so can $comments run the following block of code.

What don't you understand about the code, is it the technical aspect (what each function does) or the logical aspect?
If the former you can lookup functions and what they do at http://www.php.net