2012-02-09 22 views
1

我使用php.I創建螺紋註釋使用此代碼顯示螺紋comments.can任何一個可以告訴如何限制顯示螺紋commen indedation如何限制在PHP

我需要一個像螺紋註釋顯示這

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

請提出這個

+0

那麼你的問題是什麼?如何正確縮進?如何按照自己想要的方式獲取列表中的編號?如何向我們展示生成的HTML呢? – j08691 2012-02-09 17:21:45

+0

是的,我需要顯示在正確的縮進,我張貼 – Anish 2012-02-09 17:53:16

回答

3

的解決方案可以將第二個參數與最大深度添加到getComments你想:

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

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

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

感謝回覆。但它不創建評論我提到 – Anish 2012-02-09 17:39:19

+0

有條件的錯誤,是嗎? – solarc 2012-02-09 17:57:45

0

定義限制

$limit = 30; $count_displayed = 0; 

在getComments($ row)中增加顯示的計數並檢查是否達到限制。

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