Solved

3 column count in PHP

Posted on 2011-02-14
5
323 Views
Last Modified: 2012-05-11
My database is setup like this
Type - Identifier - Name

Currently I am displaying the Type and the count of how many times it appears.  I would like to show next to it the name that has the most occurrences of the type in the database.

This is the code I am using
echo "<table border=1><tr><td><strong>Type</strong></td><td><strong>Count</strong></td></tr>";
$query =  mysql_query("SELECT COUNT(*) as cnt, type FROM db GROUP BY type ORDER BY cnt DESC");

while ($row = mysql_fetch_array($query)) {
	echo "<tr><td>" . $row['type'] . "</td><td>" . $row['cnt'] . "</td></tr>";
}

echo "</table>";

Open in new window


How can I have the sql query count which Name has the most occurrences of type in the above code?

Thanks!
0
Comment
Question by:ScotTFO
5 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34888396
Uhh, would you want to add the column name for the "name" to the query?  Then echo $row["name"] or something like that?
0
 

Author Comment

by:ScotTFO
ID: 34888417
Yeah..just trying to add a 3rd column but not sure how to do a second count within that SQL query.
0
 
LVL 17

Expert Comment

by:Chris Harte
ID: 34888457
Sounds like the function you require is array_count_values

http://uk.php.net/manual/en/function.array-count-values.php
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34889261
Please explain this a little more.  What exactly do you want to count?  If you could give us an example it might be helpful.  Thanks, ~Ray
0
 
LVL 3

Accepted Solution

by:
wwwdeveloper2 earned 500 total points
ID: 34896185
Here is a start on how you might achieve this.  I didn't try to set up a demo table to test it.  It should give you the idea of the route I'm taking.  Basically in your while loop I'm going out and doing another query to count the names based on the type you are currently looping through.  I'm trying to order and limit the the results by 1, so you will have the name with the most counts on top and only output that.
 
   
echo "<table border=1><tr><td><strong>Type</strong></td><td><strong>Count</strong></td></tr>";
$query =  mysql_query("SELECT COUNT(*) as cnt, type FROM db GROUP BY type ORDER BY cnt DESC");

while ($row = mysql_fetch_array($query)) {
	echo "<tr><td>" . $row['type'] . "</td><td>" . $row['cnt'];
        
	$queryNames =  mysql_query("SELECT COUNT($row['name']) as nameCnt, name FROM db where type=$row['type'] order by nameCnt desc LIMIT 1");

	while ($rowName = mysql_fetch_array($queryNames)) {
	echo "<td>" . $rowName['name'] . "</td>";
	}
	echo "</td></tr>";

}

echo "</table>";

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question