Page 1 of 1

Help with a math function total

Posted: Mon Mar 29, 2004 10:00 pm
by warriors2003
Ok guys I'm trying to incorporate a table with some totals of baseball stats of all the players on a particular team. I have some nice player breakdowns, but need to figure out a way to just total a query and select a partical team then be able to display a little table with their totals. If this is what i use for the team, how would a put the particual math work for each cateogry?? I'm good with make work in submit forms but suck outside of it. Just some basic adding would work for now, if i can figure out just one total i can then figure out the math work for other complex categories such as avg.
Here is what I have:
<?

$query = mysql_query("select * from pit where team = 'Ana'");

?>


<?

$color = "#CCCCCC";
while ($row = mysql_fetch_array($query))
{

echo "<tr bgcolor=\"$color\"><td><b><img src=$row[logo]height=28 width=33</td><td><font size=2><div align=center><b><a href=pitplayer.php?pit_id=$row[pit_id]>$row[pitname]<b>$row[w]</b></td><td><font size=2><div align=center><b>$row[l]</b></td><td><font size=2><div align=center><b>$row[s]</b></td><td><font size=2><div align=center><b>$row[k]</b></td><td><font size=2><div align=center><b>$row[IP]</b></td><td><font size=2><div align=center><b>$row[ER]</b></td><td><font size=2><div align=center><b>$row[era]</b></td></tr>";


?>
------------------------

Th above code is what I use just for the filetring in the players. But i figure if i can learn to add the totals code and math work to it. I just don;t know how to go about it.

Would I put the math work in the Echo?? Or before it? And how would i change the query around if needed? I'm thinking I just need to add the math work probably to the echo???

Thanks in Advance

Posted: Tue Mar 30, 2004 6:15 am
by compound_eye
mysql will actually do a lot of these sort of things for you


SELECT SUM(homeRuns) as runSum, AVG(homeRuns) as runAvg
FROM `player`
WHERE team ='Ana'

will give you the average and the total home runs scored by the team

i'd like to explain more but it's pretty late at night where i am and i need to go to sleep

good luck

Posted: Tue Mar 30, 2004 4:51 pm
by warriors2003
Thanks for the reply!! I have been playing around with the statement but don;t really know where and how to incorporate it. Can you point me in the right direction of the how exactly to code and where to put it???..Thnkas in advance

Posted: Tue Mar 30, 2004 6:49 pm
by warriors2003
Anybody have any other thoughts on this? I've been going at it about two hours, and can;t figure out a way to get it to work,,uggghHH!!!..lol.

Posted: Tue Mar 30, 2004 6:53 pm
by Illusionist
See if this helps:

Code: Select all

$sql="SELECT SUM(homeruns) as runSum, AVG(homeruns) as runAvg FROM players WHERE team='Ana'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo $row['runSum'];
echo "<BR>";
echo $row['runAvg'];

Posted: Tue Mar 30, 2004 7:05 pm
by warriors2003
yes that worked!!!! thanks you so much!!! Now Im trying to incorporate that in some standard tables I have. It doesn;t work in that, but I think cuase i messed up the result query,,this is what I have,,what is wrong do you think???

<?

$sql="SELECT SUM(hr) as hrSum FROM hit WHERE team='Ana'";

?>
<body bgcolor="#000000">

<TABLE>
<tr>
<td bgcolor="#C4ECFF"><b>HR</b> </a> </td>

</tr>

<?

$color = "#CCCCCC";
while ($row = mysql_fetch_array($result))
{

echo "<tr bgcolor=\"$color\"><b>$row[hrSum]</b></td></tr>";

if($color == "#CCCCCC") {
$color = "#FFFFFF";
} else {
$color = "#CCCCCC";
}

}
?>
-----------------------

Yours works i tested it,,just trying to fill in the blanks with what im trying to use,,,thanks!!!

Posted: Tue Mar 30, 2004 7:21 pm
by warriors2003
I GOT IT TO WORK!!! Thanks so much guys!!! It was my bad,,my html tables were all jacked up...lol..

Anyways,,can you guys direct me to a sight with the math functions??? Cuase I'm going to need more complex functions,,

for instance,,,for avg. i'll need to make a function that adds sum ofl "h" dividing by the sum "ab". I imagine that will be complex, but I it should be able to be pulled off right?