Jump to content

List and count values in a table


simonp

Recommended Posts

Hi,

 

I thought what I was trying to do was simple but appears to be a little beyond my mysql knowledge ;)

 

I have a table (populated by a drop down box on a web page) which looks a bit like:

 

Dave

Will

Simon

Andy

Will

Simon

Dave

Dave

Chris

Dave

 

... and I need to be able to query the number of times each name appears (extra names may be added in the future to can't hard code the names).

 

So I should end up with

 

Dave - 4

Will - 2

Simon - 2

Andy - 1

 

So far I have:

 

$query = "SELECT * FROM table WHERE `fieldid` = 1 ORDER BY value ASC";
$result=mysql_query($query);
while($data = mysql_fetch_array($result)) {
$value = $data["value"];
$uses = $data["uses"];
$reportdata["tablevalues"][] = array($value,$uses);
}

 

...which just lists all the name.

 

I think I need a foreach and maybe a count (*) but am not having much joy - any help appeciated!

 

 

Simon

Link to comment
https://forums.phpfreaks.com/topic/191975-list-and-count-values-in-a-table/
Share on other sites

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.