1

我正在使用 php 创建一个线程注释。我使用此代码来显示线程注释。任何人都可以告诉如何限制显示线程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>";
}
?>

请为此提出解决方案

4

2 回答 2

3

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

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

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

    ...
        while($row = mysql_fetch_assoc($r)) {
            getComments($row, $depth - 1);
        }
    ...
}
于 2012-02-09T17:15:06.733 回答
0

定义限制

$limit = 30; $count_displayed = 0;

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

function getComments($row) {
    $count_displayed++;
    if($count_displayed >= $limit) return;
    ...
}
于 2012-02-09T17:16:16.197 回答