Dreni
Dreni

Reputation: 161

Summarize php result

So far i got this code:

function toplist() {$sql = "SELECT * FROM list WHERE date=curdate()";
$result = mysql_query($sql);
$num= mysql_numrows($result);
if ( mysql_num_rows($result) ) {
$i=0;

while( $i < $num) {
$user = mysql_real_escape_string(mysql_result($result, $i, "user"));
$todayscore = mysql_real_escape_string(mysql_result($result, $i, "todayscore"));

echo '
'.mysql_real_escape_string(mysql_result($result, $i, "user")).' 
'.mysql_real_escape_string(mysql_result($result, $i, "todayscore ")).' points
<br/>';

$i++;
    }   
    }}

This results in a list like this:
User two 200 points
User one 300 points
User two 150 points
User two 100 points

Now I would like it to summarize like this (from the example above):
User two 450 points
User one 300 points

And if possible, arange so that the user with the most points gets on top the others.

Thanks in advance.

Upvotes: 0

Views: 174

Answers (1)

janosrusiczki
janosrusiczki

Reputation: 1931

SELECT SUM(todayscore) AS points, user FROM list WHERE date = curdate() GROUP BY user ORDER BY points DESC

Upvotes: 2

Related Questions