Jump to content

[SOLVED] Is there a simpler way to do these queries?


AStrangerWCandy

Recommended Posts

So I have a site with a private member system. When users register they indicate their country. This is stored in it's own column in a mySQL database and is recalled later to display a little flag on their user profiles.

 

I'd like to create a site stats page that shows the # of users from any particular country. There are 200+ countries listed as options to choose from. Is there a way to create a page that says the count for each country type without doing 200+ count SQL queries? (Meaning could I do one query and then sort through that data after having done the query)

Link to comment
Share on other sites

Assuming you're using MySQL:

$query = "SELECT countryName, COUNT(userID) as users
  FROM userTable
GROUP BY countryName";

$result = mysql_query($query);

echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
    echo '<tr>><td><img src="'.$row['countryName'].'.jpg"> '.$row['countryName'].'</td>';
    echo '<td>'.$row['users'].' Members</td></tr>';
}
echo '</table>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.