MartynLearnsPHP Posted November 30, 2014 Share Posted November 30, 2014 Hi, I am trying to make a list of all countries listed in a database and the number of times they appear. But I am clearly doing something wrong as I am not able to list out the number of times that they appear. This is what I have got so far: <h4>Countries</h4> <table><col width='150px'><col width='150px'> <tr> <th> Country </th> <th> Number of Occurences </th> </tr> <?php $location = DB::getInstance()->query("SELECT country, COUNT(country) as countCountry FROM users GROUP BY country ORDER BY count DESC"); foreach ($location->results() as $locations) { echo "<tr><td>"; echo $locations->country; echo "</td><td>"; echo $locations->countCountry; echo "</td></tr>"; } ?> </table> Can anyone point me in the right direction? Quote Link to comment https://forums.phpfreaks.com/topic/292813-table-columns-and-counting/ Share on other sites More sharing options...
Solution Ch0cu3r Posted November 30, 2014 Solution Share Posted November 30, 2014 ORDER BY count should be ORDER BY countCountry Quote Link to comment https://forums.phpfreaks.com/topic/292813-table-columns-and-counting/#findComment-1498121 Share on other sites More sharing options...
Barand Posted November 30, 2014 Share Posted November 30, 2014 (edited) Try $location = DB::getInstance()->query("SELECT country, COUNT(country) as countCountry FROM users GROUP BY country ORDER BY count DESC"); while ($locations = $location->fetch_object()) { echo "<tr><td>"; echo $locations->country; echo "</td><td>"; echo $locations->countCountry; echo "</td></tr>"; } edit : Plus what Ch0cU3r said. Edited November 30, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/292813-table-columns-and-counting/#findComment-1498122 Share on other sites More sharing options...
MartynLearnsPHP Posted November 30, 2014 Author Share Posted November 30, 2014 Fantastic. Amending my ORDER BY has resolved my problem. Thanks you guys. Your help is very much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/292813-table-columns-and-counting/#findComment-1498123 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.