Using AVG function

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

User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

post the code your now using

seems you didnt define the variables, you need this script:

$h = $row['hSum'];
$ab = $row['abSum'];
warriors2003
Forum Commoner
Posts: 38
Joined: Wed Mar 24, 2004 7:24 pm

Post by warriors2003 »

<?php
$h = $row['hSum']; // ills code
$ab = $row['abSum'];
//this can be anywhere on the page after the mysql_fetch_array cause the fetch_array helps point the row grabbing

// the code you have:
echo "<tr bgcolor=\"$color\"><td><b>$row[avgAvg]</b></td><td><b>$row[hSum]</b></td><td><b>$row[abSum]</b></td><td><b>$row[hrSum]</b></td><td><b>$row[rbiSum]</b></td><td><b>$row[rSum]</b></td><td><b>$row[doublesSum]</b></td><td><b>$row[triplesSum]</b></td><td><b>$row[sbSum]</b></td>"; // ends your echo, but not your table
echo "<td><b>$h/$ab; </b></td></tr>"; // the new echo that incorperates in your table with ills variable.
?>


Thanks again for trying to help!!
warriors2003
Forum Commoner
Posts: 38
Joined: Wed Mar 24, 2004 7:24 pm

Post by warriors2003 »

When it displays the data,,,it has 2/13

It shows the total hits with the division sign then the total at bats,,but it isn;t actually doing the math and then displaying it for output,,,I think we are really close tho!!!
warriors2003
Forum Commoner
Posts: 38
Joined: Wed Mar 24, 2004 7:24 pm

Post by warriors2003 »

HOLY GEESE!!! I GOT IT!! Don;t know how i pulled it off,,,prolly with alot of luck!!!

I added this to the bottom of Ill's code
$row[avg] = $row['hSum']/$row['abSum'];


<?php
$h = $row['hSum']; // ills code
$ab = $row['abSum'];
$row[avg] = $row['hSum']/$row['abSum'];

Then just used a regular echo with a row being [avg]

THAT SEEMED TO DO IT!!! Thanks guys!!!
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

well congrats

i'm happy that not only the problem is corrected, but I was wrong and you was right

Excellent job
warriors2003
Forum Commoner
Posts: 38
Joined: Wed Mar 24, 2004 7:24 pm

Post by warriors2003 »

You guys were right all along!!! I wouldn;t have been able to get it anywhere close to figure out if it wasnt for you guys!!!

But one quick question....I remember you helped me with the rounding function in the sql yesterday, but since the parameters have now changed in the code, i've been palying around with it and can't get it to round,,,again..lol.

This is what you gave me yesterday

"SELECT SUM(h) as hSum, round(avg(Avg), 3) as avgAvg blah blah

And that worked!!!

But now the paramter has changed to just "avg" and its not in the database somethign I can select, its a total i put math work too,,,,,,it might have to be moved to be rounded somwhere else in the code,,,,cause I tried changing the above code a million different ways to fit avg, can't get it to round!!!

Any thoughts??
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

well look at the other thread, you'll get it.

hint: you wont use the sql version provided by wierdan =]

also, php.net is your bible.

http://www.php.net/manual/en/function.round.php
warriors2003
Forum Commoner
Posts: 38
Joined: Wed Mar 24, 2004 7:24 pm

Post by warriors2003 »

Mission accomplished Jedi Master....
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

you dont ahve to use $row['avg'] to assign that to, you can jsut assing it to $avg.

Code: Select all

$avg = $hSum / $abSum;
Post Reply