2012-05-07 10 views
5

usersテーブルカウントマルチレベルマーケティング(木)レコード

enter image description here

、すべてのユーザーが登録PARENT_ID下PARENT_IDを配置する必要があり、登録時に

ので、私はそれ

のための別のテーブルを作りました

sponserテーブル

enter image description here

それはそのようなツリーを行った後

enter image description here

は、と私はそのようにレコードをカウントするか、私はこの種の言うことを意味どのような方法があることができますどのようにplzは私を導いている

enter image description here

のようなレコードをカウントします私はデータベースで変更する必要があります、事前に感謝

+0

どのようにツリー構造を[上の画像](http://i.stack.imgur.c)のように出力しましたか? om/p4XdG.gif)?? –

答えて

6

iは、テーブル内の隣接関係モデル構造を持っているので、私は持って非常に素晴らしいソリューション回数のuser_id PARENT_ID下PARENT_ID下user_idを数える機能

function display_children($parent, $level) { 
    $count = 0; 
    $result = mysql_query('SELECT user_id FROM sponsers '.'WHERE parent_id="'.$parent.'"'); 
    while ($row = mysql_fetch_array($result)) 
    { 
      $var = str_repeat(' ',$level).$row['user_id']."\n"; 

        //echo $var after remove comment check tree 

        // i call function in while loop until count all user_id 

      $count += 1 +$this->display_children($row['user_id'], $level+1); 

    } 
    return $count; // it will return all user_id count under parent_id 
} 

コール機能

display_children(999, 0) 
+0

あなたは私がcodeigniterでどうやってできるか教えてくれますか? –