phpthreaded-comments

How to limit threaded comment display in php


i am creating a threaded comment using php.I using this code to display the threaded comments.can any one tell how to limit the displaying the threaded commen indedation

I need like this

comment 1
 -->comment 1.1
 -->comment 1.1.1
 -->comment 1.2
 -->comment 1.2.1
 -->comment 1.2.2
comment 2
  -->comment 2.1
  -->comment 2.1.2

but not like this

comment 1
 -->comment 1.1
   -->comment 1.1.1
 -->comment 1.2
    -->comment 1.2.1
      -->comment 1.2.1.1
comment 2
  -->comment 2.1
    -->comment 2.1.2

My php code is like this

<div id='wrapper'>
<ul>
<?php
$q = "select idvideo,discussion from video_discussion where 
                     video_discussion.idvideo = 972 AND parent_id = 0
$r = mysql_query($q);
while($row = mysql_fetch_assoc($r)):
    getComments($row);
endwhile;
?>
</ul>

and on function page

<?php
function getComments($row) {
    echo "<li class='comment'>";
    echo "<div class='aut'>".$row['discussion']."</div>";       
    echo "<a href='#comment_form' class='reply' id='".$row['idvideo_discussion']."'>Reply</a>";

$q=" select idvideo,discussion from video_discussion where video_discussion.idvideo = 972 and  parent_id =".$row['idvideo_discussion'].";   

    $r = mysql_query($q);
    if(mysql_num_rows($r)>0)
        {
        echo "<ul>";
        while($row = mysql_fetch_assoc($r)) {
            getComments($row);
        }
        echo "</ul>";
        }
    echo "</li>";
}
?>

please suggest a solution for this


Solution

  • You can add a second parameter to getComments with the max depth you want:

    function getComments($row, $depth=3)
    {
        echo ...
    
        if (0 === $depth) {
            return;
        }
    
        ...
            while($row = mysql_fetch_assoc($r)) {
                getComments($row, $depth - 1);
            }
        ...
    }