Jump to content

Average per level calculation


EchoFool

Recommended Posts

I am having difficult on the logic of how to make a query to count the average money each level of users have... so the output would look like:

 

Level 1 : Average = £5.00

Level 2 : Averag = £4.50

 

etc

 

So far i have got to :

 

<?php
$Get = mysql_query("SELECT Level,Money FROM users GROUP BY Level ORDER BY Level ASC")
	Or die(mysql_error());
$Total = mysql_num_rows($Get);

While($row = mysql_fetch_assoc($Get)){
        $Level = $row['Level'];
$Money = $Money + $row['MoneyInHand'];
$MoneyAvg = $Money / $Total;
Echo 'Level '.$Level.' average money '.$MoneyAvg;	
Echo '<br><br>';	
}



?>

 

But have realized it is totally wrong ! So am kinda stuck. Please help.

Link to comment
https://forums.phpfreaks.com/topic/118311-average-per-level-calculation/
Share on other sites

that is, perform a query that looks like this:

 

SELECT Level, AVG(Money) AS average_money FROM users GROUP BY Level ORDER BY Level

 

MySQL often has functions that take the work out of your PHP code, you should have a look at that chapter in the MySQL manual.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.