2012-03-18 155 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