ordering results from db

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jmandango
Forum Newbie
Posts: 19
Joined: Wed Jun 05, 2002 10:39 am
Location: Michigan

ordering results from db

Post by jmandango »

Okay, How can I sort results from a mySql table using PHP so that multiple fields are grouped and then sorted from largest group to smallest.

this is what the results would look like:

----------------------
colors
----------
red
red
red
red
blue
blue
green
Wandrer
Forum Newbie
Posts: 21
Joined: Thu Jun 06, 2002 8:43 am

Post by Wandrer »

have you tried using 'group by' or 'order by' in your SQL statement ?
jmandango
Forum Newbie
Posts: 19
Joined: Wed Jun 05, 2002 10:39 am
Location: Michigan

Post by jmandango »

Code: Select all

<html>
<body>
<?php

$db = mysql_connect("localhost", "user", "pass");

mysql_select_db("mydb");

$result = mysql_query("SELECT the_color, COUNT(the_color) FROM the_table ORDER BY MAX;");

echo "<table border=1 class=graybody>";

echo "<tr bgcolor=999999><td>The Color</td></tr>";

while ($Row = mysql_fetch_array($result)) &#123;

	printf("<tr>
				<td>$the_color</td></tr>\n",
	
	$the_color = $Row&#1111;'the_color']);

&#125;
	
echo "</table>\n";

?>
</body>
</html>
Here is my code. I have several fields in the table that will have a variety of different entries. I want to be able to click on each field header on a page which will then do a query which will group the results of the list (i.e. put all the reds together) and then put them in descending order. (the most reds, followed by blues, greens, etc.) I might even want to have the number of each color in a table next to each color (i.e. red - 5, blue - 3, green - 1) Help please, I'm stuck.
Post Reply