Solved

3 column count in PHP

Posted on 2011-02-14
5
314 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 108

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 16

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 108

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Healthcheck 2 81
Store image url and display it for user profile 6 23
Should I use subdomains or addon domains? 3 33
Session timeout 5 13
Creating and Managing Databases with phpMyAdmin in cPanel.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

943 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now