Need help understanding threaded comments code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nycitservices
Forum Newbie
Posts: 1
Joined: Tue Aug 02, 2005 10:14 am

Need help understanding threaded comments code

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

?>
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
Post Reply