2012-04-29 97 views
3

我想diplay在我的網站的評論是這樣的:如何實現嵌套註釋?

<li>Parent 
    <ul> 
     <li>child one</li> 
     <li>child two 
      <ul> 
       <li>grandchild</li> 
       <li>other grandchild</li> 
      </ul> 
     </li> 
    </ul> 
<li>Another parent with no children</li> 
<li> 

我已閱讀the following article,但它不使用<li>。那麼有沒有辦法顯示像我之前做過的數組這樣的評論?謝謝。

$comments = array(
     array('id'=>1, 'parent_id'=>NULL, 'text'=>'Parent'), 
     array('id'=>2, 'parent_id'=>1,  'text'=>'Child'), 
     array('id'=>3, 'parent_id'=>2,  'text'=>'Child Third level'), 
     array('id'=>4, 'parent_id'=>NULL, 'text'=>'Second Parent'), 
     array('id'=>5, 'parent_id'=>4,  'text'=>'Second Child') 
); 
+2

是的,這是完全可能的。你只需要創建*遞歸*函數*構建樹*。 (Google,搜索那個......) – deceze 2012-04-29 08:57:38

+0

你也可以*展開*這個樹結構到一個普通的列表中,然後用簡單的foreach輸出它 – 2012-04-29 09:00:38

+0

可能的[如何將一系列父子關係轉換成分層樹?](http://stackoverflow.com/questions/2915748/how-can-i-convert-a-series-of-parent-child-relationships-into-a-hierarchical-tre) - 這已被問到之前[和回答](http://stackoverflow.com/a/8285070/367456)。將數組結構轉換爲嵌套集合等其他結構也很有用。 – hakre 2012-04-29 09:09:58

回答

4

我asssume您的評論表有ID,PARENT_ID,評論,...和我的建議是這樣的;

請選擇您評論like;

$sql = "SELECT *FROM comments ORDER BY id DESC"; 

$rows = mysql_query($sql); 

而下一步就是數組operations.You可以看到下面的下面的代碼,並嘗試工作演示here;

$rows = your_select_result;//I assumed that you have done these stuffs 
$comments = $row; 
/** 
This is test data, please remove this array while you are 
running own application.Since you will use the data one you get your db 
**/ 
$comments = array(
    1 => array('id' => 1, 'parent_id' => 0, 'childs' => array()), 
    2 => array('id' => 2, 'parent_id' => 0, 'childs' => array()), 
    3 => array('id' => 3, 'parent_id' => 0, 'childs' => array()), 
    5 => array('id' => 5, 'parent_id' => 0, 'childs' => array()), 
    11 => array('id' => 11, 'parent_id' => 0, 'childs' => array()), 
    17 => array('id' => 17, 'parent_id' => 0, 'childs' => array()), 
    23 => array('id' => 23, 'parent_id' => 0, 'childs' => array()), 
    28 => array('id' => 28, 'parent_id' => 0, 'childs' => array()), 
    4 => array('id' => 4, 'parent_id' => 1, 'childs' => array()), 
    6 => array('id' => 6, 'parent_id' => 1, 'childs' => array()), 
    8 => array('id' => 8, 'parent_id' => 2, 'childs' => array()), 
    9 => array('id' => 9, 'parent_id' => 2, 'childs' => array()), 
    7 => array('id' => 7, 'parent_id' => 3, 'childs' => array()), 
    12 => array('id' =>12, 'parent_id' => 7, 'childs' => array()), 
    13 => array('id' => 13, 'parent_id' => 12, 'childs' => array()), 
); 

/** Comment prepare start */ 
foreach ($comments as $k => &$v) { 
    if ($v['parent_id'] != 0) { 
     $comments[$v['parent_id']]['childs'][] =& $v; 
    } 
} 
unset($v); 

foreach ($comments as $k => $v) { 
    if ($v['parent_id'] != 0) { 
     unset($comments[$k]); 
    } 
} 

/** Comment prepare end */ 

//Your indent pattern 
function indent($size) { 
    $string = ""; 
    for ($i = 0; $i < $size; $i++) { 
     $string .= "#"; 
    } 
    echo $string; 
} 


function printComments($comments, $indent = 0) { 
    foreach ($comments as $comment) { 
     echo indent($indent + 1).' I am comment '.$comment['id']."\n"; 
     if (!empty($comment['childs'])) { 
      printComments($comment['childs'], $indent + 1); 
     } 
     } 
} 


printComments($comments); 

對於演示,請參閱here

+0

適用於二維層次結構,但對於任意嵌套的條目不起作用,如此處所述。 – deceze 2012-04-30 00:44:06

+0

是的,你說得對。我改變了代碼的邏輯(不是很多),並更新了代碼。我還添加了工作demo.Please看到更新 – 2012-04-30 09:29:01

1

BTW,在使用物化路徑技術的情況下,你將不再需要任何的遞歸和嵌套數組或東西。

從數據庫中只是普通的線性輸出。

要做到這一點,只需在您的數據庫中創建一個名爲path的字段,並填寫所有父ID,填充一些相當的長度。

說,例如樹可能看起來像

id 1 root path 
    id 3 root 1 path 000000001 
     id 5 root 1 path 000000001000000003 
    id 4 root 1 path 000000001 
id 2 root path 000000002 
    id 6 root 2 path 

因此,通過簡單的ORDER BY root DESC, path ASC
查詢你的表,你會得到你的樹作爲一個簡單的已排序列表

0

此功能只需要parent_idid鍵存在於每個評論的數組中。

$comments = array(
      array('id'=>1, 'parent_id'=>NULL, 'text'=>'Parent'), 
      array('id'=>2, 'parent_id'=>1, 'text'=>'Child'), 
      array('id'=>3, 'parent_id'=>2, 'text'=>'Child Third level'), 
      array('id'=>4, 'parent_id'=>NULL, 'text'=>'Second Parent'), 
      array('id'=>5, 'parent_id'=>4, 'text'=>'Second Child') 
     ); 

這將返回一個多維數組。如果一個項目沒有子女,$comment['children']將等於NULL,否則將附加相應的子項數組。

function arrangecomments($comments){ 

    $tree = array(); 

    /* We get all the parent into the tree array */ 
    foreach ($comments as &$node) { 
     /* Note: I've used 0 for top level parent, you can change this to == 'NULL' */ 
     if($node['parent_id']=='0'){ 
      $tree[] = $node; 
      unset($node); 
     } 
    } 

    /* This is the recursive function that does the magic */ 
    /* $k is the position in the array */ 
    function findchildren(&$parent, &$comments, $k=0){ 
     if (isset($comments[$k])){ 
      if($comments[$k]['parent_id']==$parent['id']){ 
       $com = $comments[$k]; 
       findchildren($com, $comments); // We try to find children's children 
       $parent['children'][] = $com; 
      } 
      findchildren($parent, $comments, $k+1); // And move to the next sibling 
     } 
    } 

    /* looping through the parent array, we try to find the children */ 
    foreach ($tree as &$parent) { 
     findchildren($parent, $comments); 
    } 

    return $tree; 

} 

我知道這是可以提高了很多,但它的作品,我還沒有發現任何錯誤爲止。希望能幫助到你!