2012-03-18 149 views
0

我有三張表my_location1 & my_location2 & my_location3。 my_ is Prefix。位置1 =鄉村主義者。 location2 = statslist。 location3 =城鎮名單。在其他表中,我有用戶列表。現在我想列出在每個國家統計鎮(計數)中使用樹木樣式+水族用戶的所有位置(國家/地區 - 城鎮)。 (如何生成這個?)國家/城市/城鎮+計數列表

DATABSE注:PID & PID1 =國家,PID2 =的ID統計的ID

my_location1:

id ++++++ name ++++++ active 

my_location2:

id ++++++ pid +++++++ name ++++++ active 

my_location3:

id ++++++ pid1 +++++++ pid2 ++++++ name ++++++ active 

my_user

id ++++++ name ++++++ location1 ++++++ location2 ++++++ location3 ++++++ active 

e.x

Country (print count Of users in this country) 
+++++++stats (print count Of users in this stats) 
++++++++++++town (print count Of users in this town) 
++++++++++++town (...) 
++++++++++++town (...) 
++++++++++++town (...) 
+++++++stats (...) 
++++++++++++town (...) 
+++++++stats (...) 
++++++++++++town (...) 
++++++++++++town (...) 
Country1 (...) 
+++++++stats (...) 
++++++++++++town (...) 
++++++++++++town (...) 
Country2 (...) 
+++++++stats (...) 
++++++++++++town (...) 
++++++++++++town (...) 
++++++++++++town (...) 
++++++++++++town (...) 
...more 

感謝您的幫助。

+0

那麼你的問題到底是什麼? – 2012-03-18 10:11:39

+0

我想列出在每個國家統計鎮(計數)中使用樹木樣式+水族用戶的所有位置(國家/地區 - 城鎮)。 (如何生成這個??) – 2012-03-18 10:13:55

+0

取決於你想要使用哪個樹視圖控件。你是否已經選擇了一個樹形視圖控件,例如。 http://bassistance.de/jquery-plugins/jquery-plugin-treeview/? – 2012-03-18 14:09:08

回答

0

我沒有檢查的代碼,但它應該是這樣的:

function getCountries() 
{ 
    return mysql_query('SELECT c.*, (COUNT(*) from u WHERE u.location1 = c.id) AS user_count 
          from countrylist c 
          LEFT JOIN users u 
          ON u.location1 = c.id 
          WHERE c.active = 1' 
         ); 
} 

function getStats($countryId) 
{ 
    return mysql_query('SELECT s.*, (COUNT(*) from u WHERE s.location2 = s.id) AS user_count 
          from statslist s 
          LEFT JOIN users u 
          ON u.location2 = s.id 
          WHERE s.pid = ' . $countryId 
         ); 
} 

function getTowns($stateId) 
{ 
    return mysql_query('SELECT t.*, (COUNT(*) from u WHERE t.location3 = t.id) AS user_count 
          from townlist 
          LEFT JOIN users u 
          ON u.location3 = t.id 
          WHERE t.pid2 = ' . $stateId 
         ); 
} 

echo "<ul>\n"; 
while ($country = mysql_fetch_assoc(getCountries())) { 
    echo "<li>" . $country["name"] . "(" . $country["user_count"] . ")\n"; 
    echo "<ul>\n"; 

    while ($state = mysql_fetch_assoc(getStats($country["id"]))) { 
     echo "<li>" . $state["name"] . "(" . $state["user_count"] . ")\n<ul>\n"; 
     while ($town = mysql_fetch_assoc(getTowns($state["id"]))) { 
     echo "<li>" . $town["name"] . "(" . $town["user_count"] . ")</li>\n"; 
     } 
     echo "</ul>\n</li>\n"; 
    } 

    echo "<ul>\n</li>\n"; 
} 
echo "</ul>\n"; 

它可以而且應該進一步優化,但希望你的想法。另請注意,你的mysql結構是相當多餘的。

+0

它對你有幫助嗎? – 2012-03-24 13:06:20