2012-04-29 4 views
3

私はこのように自分のサイトにコメントをdiplayしたい:私はthe following articleを読んだネストされたコメントを実装するにはどうすればよいですか?

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

、しかしそれは<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、そのためにSOを検索する...) – deceze

+0

このツリー構造をプレーンなリストに展開してから、単純なforeachを使って出力することもできます –

+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

答えて

4

あなたのコメント表にはid、parent_id、commentなどがあります。私の提案はこのようになります。

あなたのコメントをお寄せください。

$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

2次元階層には最適ですが、ここで求められているように、任意にネストされたエントリについては全く機能しません。 – deceze

+0

ええ、あなたは正しいです。私はコードのロジック(あまり)を変更し、コードを更新しました。また、私は作業デモを追加しました。 –

1

を参照してください。

データベースからの単純な線形外挿です。

これを行うには、データベースに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 

ので、あなたはシンプルすでに順序付けられたリスト

0

としてあなたのツリーを取得する簡単なORDER BY root DESC, path ASC
して、テーブルを照会この機能は、parent_id必要があります。各コメントの配列にはidキーが存在します。

$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') 
     ); 

これは、多次元配列を返します。 1つのアイテムに子がない場合、$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; 

} 

私はそれが多くのことを改善することができます知っているが、それは動作しますが、私はこれまでの任意のバグを発見していません。それが役に立てば幸い!

関連する問題