How to group results from query, in this way:

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gath
Forum Newbie
Posts: 20
Joined: Wed May 15, 2002 8:36 pm

How to group results from query, in this way:

Post by Gath »

Hi.

Having 10 database cols, in wich could have (or not) any letter, from A to J.
I need to get how many i have from each letter.
i.e: if it would be like this: A - A - C - x -D - A - E - J - E - A
(x means there's no entry).
So, i would have 4-A, 2-E, 1-C, ... ...

Using array_count_values() i get the result above (not ordered), but i need to now wich one is the letter that has "more votes"...

I looked for a function, but can't find it... and i'm stuck... can't find a way to do it...


So, my request for help is:

How can i find the "most voted" letter, so i can use it?

Thanks.
Gath
Forum Newbie
Posts: 20
Joined: Wed May 15, 2002 8:36 pm

Post by Gath »

Ok, i somewhat got a working way... but it looks kind of "rude".
Any suggestions?


Code: Select all

// Get letter votes
$vot = mysql_query("SELECT ...
$vot = mysql_fetch_row ($vot);

// Get them in quantity
$vot = array_count_values($vot);

// Need that result
$re = $vot;
		
// Copyed from manual :)
function order ($a, $b)
  {
  if ($a == $b) return 0;
  return ($a > $b) ? -1 : 1;
  };

// Getting the max. votes
usort ($re, "order");

// Dysplaying letters, and the winner gets marked with 'Letter'
for ($a=1;$a<=10;$a++)
  &#123;
  if (empty($vot&#1111;$a])) $vot&#1111;$a] = 0;
  if ($re&#1111;0] == $vot&#1111;$a]) $teet = 'Letter';
    else $teet = '';
  echo "<br>$vot&#1111;$a] $teet";
  &#125;;
Post Reply