Jump to content

Table Columns and Counting


MartynLearnsPHP

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/292813-table-columns-and-counting/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.