Page 1 of 1

How to group results from query, in this way:

Posted: Mon Jun 03, 2002 1:51 pm
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.

Posted: Mon Jun 03, 2002 5:00 pm
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;;