2011-08-22 62 views
1

我有一個帶有參考系統的網站,我希望用戶能夠使用樹視圖來查看他們引用的用戶。如何在PHP中構建推薦系統?

我的數據庫表已設置好,以便當用戶使用他的參考代碼 引用某人時,新用戶將獲得一個ID,一個贊助商代碼(來自他的「贊助者」的參考代碼,也就是讓他參與進來的人站點)和一個referal代碼(他自己的代碼讓其他人加入他之下)。

我不知道如何我可以從我的MySQL數據庫,並進入樹視圖腳本得到這個信息。

我需要能夠讓用戶看到他提到的所有人,10級深。

這是可能的嗎?我該怎麼做?

回答

0

你應該給看看分層數據(http://www.sitepoint.com/hierarchical-data-database/

<?php 

    function tree_view($index) 
    { 
     $q = mysql_query("SELECT * FROM table_name WHERE SCode=$index"); 
     if (!mysql_num_rows($q)) 
      return; 
     echo '<ul>'; 
     while ($arr = mysql_fetch_assoc($q)) 
     { 
      echo '<li>'; 
      echo $arr['UserID']; //you can add another output there 
      tree_view($arr['RCode']); 
      echo '</li>'; 
     } 
     echo '</ul>'; 
    } 

    mysql_connect('localhost', 'root', ''); 
    $link = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error()); 
    mysql_select_db('test') or die('Could not select database'); 

    tree_view(11111); 
+0

我喜歡在學說ORM嵌套組行爲這種關係 –

+0

的是什麼了$指數代表什麼?和tree_view(11111)有什麼關係呢? – Christoph

+0

$ index是傳遞給查詢以獲取由$ index贊助的所有用戶的用戶的SCode,並且Tree_view是生成贊助樹 –