如何限制php中的线程注释显示

时间:2012-02-09 17:09:04

标签: php threaded-comments

我正在使用php.I创建线程注释使用此代码显示线程comments.can任何一个告诉如何限制显示线程commen indedation

我需要这样

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

但不喜欢这个

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

我的PHP代码就像这样

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

和功能页面

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

请为此建议解决方案

2 个答案:

答案 0 :(得分:3)

您可以使用所需的最大深度为getComments添加第二个参数:

function getComments($row, $depth=3)
{
    echo ...

    if (0 === $depth) {
        return;
    }

    ...
        while($row = mysql_fetch_assoc($r)) {
            getComments($row, $depth - 1);
        }
    ...
}

答案 1 :(得分:0)

定义限制

$limit = 30; $count_displayed = 0;

在getComments($ row)中增加显示的计数并检查它是否达到限制。

function getComments($row) {
    $count_displayed++;
    if($count_displayed >= $limit) return;
    ...
}