Jump to content

[SOLVED] Average out of 10


Canman2005

Recommended Posts

Hi all

 

Wondering if someone can help.

 

I have the following query

 

$sql = "SELECT * FROM `levels`";
$show = @mysql_query($sql,$connection) or die(mysql_error());
while ($row = mysql_fetch_array($show))
{
print $row['levelcode'].'<br>';
}

 

which returns a list of level codes, the output at the moment looks like

 

72

234

4

7655

43

 

What I want to do is display next to each code returned, an average score out of 10 compared to the other codes.

 

I don't 100% know to write the query or even what the result should be, but it would look something like

 

72 - (2.6)

234 (7.4)

4 (0.4)

7655 (9.9)

43 (1.4)

 

Of course those numbers are incorrect, hence why I am trying to work out the query to use.

 

I thought maybe it would be something like

 

 

select count(*) as cnt, SUM(levelcode) as sum, (SUM(levelcode)/count(*)) as avg FROM levels;

 

but that doesnt stick to an average of 10, plus it gives an average for the whole database and not each row individually, plus it gives 4 digits result after the fullstop, such as 27.9375, whereas I need to stick to 1 digit such as 5.4

 

Could someone please help me work this one out, it's slowly making me dumber :)

 

Thanks

 

Dave

Link to comment
https://forums.phpfreaks.com/topic/118666-solved-average-out-of-10/
Share on other sites

Some accurate sample data would be ideal. It's hard to help you when we don't know what you want.

 

Example:

 

Source Data

200
150
90
50
10

 

Expected Results

200 (10)
150 (7.5)
90 (4.5)
50 (2.5)
10 (0.5)

 

 

The algorithm I'm assuming you're gonna need is something like this

 


$base = 10;

$ratio = $base / max($values);

foreach ( $values as $value )
   echo $value .' ('. ( $value * $ratio ) .')<br />';

alternatively, if you're simply looking for the proportion the number represents from the whole (rather than comparing it to the max value), you can do the following:

 

SELECT levelcode, levelcode/SUM(levelcode) AS fraction FROM levels

 

to get the fraction of ten it represents, simply multiply that decimal by 10:

 

while (stuff = stuff)
{
  echo $stuff['levelcode'].' - '.round($stuff['fraction'] * 10, 2).'<br />';
}

 

i have to caution that i don't know whether MySQL will like the use of an aggregate function without a group by, but it's worth a shot.  if MySQL complains, run the levelcode query first and store in an array, then calculate the sum of the array and echo much like disco has suggested.

SELECT levelcode FROM levels

$levels = array();
while (stuff = stuff)
{
  $levels[] = $stuff['levelcode'];
}

$total = array_sum($levels);
foreach ($levels AS $value)
{
  echo $value.' = '.round($value / $total, 2).'<br />';
}

 

give that a whirl.  of course, you'll need to make it syntactically correct.

Hi all

 

I tried altering it to

 

$stuff  "SELECT levelcode FROM levels";
$levels = array();
while ($stuff = $stuff)
{
  $levels[] = $stuff['levelcode'];
}

$total = array_sum($levels);
foreach ($levels AS $value)
{
  echo $value.' = '.round($value / $total, 2).'<br />';
}

 

not sure if I did that right, but it doesnt seem to produce any result at all.

 

Can anyone help?

Read the while loop condition and tell me if it makes sense. =P  You also never even execute the query.

 

while ($stuff = $stuff) {

 

xD  Change it to:

 

$sql = "SELECT levelcode FROM levels";

$result = mysql_query($sql) OR die(mysql_error());

$levels = array();

while ($row = mysql_fetch_assoc($result))

{

  $levels[] = $row['levelcode'];

}

$total = array_sum($levels);

foreach ($levels AS $value)

{

  echo $value.' = '.round($value / $total, 2).'<br />';

}

My data

[pre]

mysql> SELECT x FROM numbers;

+----+

| x  |

+----+

| 20 |

|  2 |

|  8 |

|  4 |

|  5 |

| 21 |

| 25 |

+----+[/pre]

 

<?php
$maxres = mysql_query ("SELECT MAX(x) FROM numbers");
$max = mysql_result ($maxres,0);

$sql = "SELECT x, x*100/$max as pc FROM numbers";
$res = mysql_query($sql);
while (list($x, $pc) = mysql_fetch_row($res))
{
    printf('%d (%0.1f%%)<br/>', $x, $pc);
}			
?>

-->
20 (80.0%)
2 (8.0%)
8 (32.0%)
4 (16.0%)
5 (20.0%)
21 (84.0%)
25 (100.0%)

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.