简体   繁体   中英

list of country/stats/town + count

i have three table for my_location1 & my_location2 & my_location3 . my_ is Prefix . location1 = countrylist . location2 = statslist . location3 = townlist . in other table i have userlist . now i want to list all Location (country - stats - town) using tree styles + aqual user in each country stats town (count). ( How To Generate This ?? )

Databse NOTE : pid & pid1 = id of country, pid2 = id of stats

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

ex

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

Thanks for your help.

I didn't check the code, but it should be something like this:

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

It could and should be further optimized, but hope you get the idea. Please also note that your mysql structure is quite redundant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM