Forum Moderators: coopster

Message Too Old, No Replies

adding up variables

         

drollz

10:39 pm on Sep 7, 2003 (gmt 0)

10+ Year Member



Hello, again... Thank you to everyone who has helped me in the past.

My latest question deals with variables once again. I have a table with values stored for multiple rows. Example: points scored (value in column) for players (rows) in a table (list of players). If I display a specific (by team, by position, by hair color, etc.) list of players and their points scored, how can i find the sum of their points scored? This should be simple but it is killing me! Because when I extract "pts" (column) as $pts(variable), i cannot get the sum because each $pts overwrites the previous one.

Please keep in mind that i am a humble beginner, so be as specific as possible with your examples. Thanks in advance!

justageek

11:21 pm on Sep 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you sticky me or, post it here, the part that is getting the info from the db and displaying it I'll take a look at what you're trying to do. Can't promise I have the answer but I'll look :-)

jatar_k

12:22 am on Sep 8, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If I display a specific list of players and their points scored, how can i find the sum of their points scored?

sorry drollz, you totally lost me. What data do you have presently and what would you like to do with it?

justageek

1:40 am on Sep 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I haven't seen the specifics of the code yet but I'm thinking you need to do something like "select distinct player as player and sum(points) as points where player = 'playername'". That will give you one player name in a variable called player and all of that players points in a variable called points.

drollz

5:23 am on Sep 8, 2003 (gmt 0)

10+ Year Member



$getdata = "select * from 200001playerstats where playerid = $activeplayer";
$result = mysql_query($getdata);
while ( $row = mysql_fetch_array($result))
{
extract($row);

$fgpct = $fgm / $fga;
$f_fgpct = sprintf("%.3f", $fgpct);
echo "$year $fgm-$fga ($f_fgpct)<br>";
}

$getdata = "select * from 200102playerstats where playerid = $activeplayer";
$result = mysql_query($getdata);
while ( $row = mysql_fetch_array($result))
{
extract($row);
$fgpct = $fgm / $fga;
$f_fgpct = sprintf("%.3f", $fgpct);
echo "$year $fgm-$fga ($f_fgpct)<br>";
}
$getdata = "select * from 200203playerstats where playerid = $activeplayer";
$result = mysql_query($getdata);
while ( $row = mysql_fetch_array($result))
{
extract($row);
$fgpct = $fgm / $fga;
$f_fgpct = sprintf("%.3f", $fgpct);

echo "$year $fgm-$fga ($f_fgpct)<br>";
}

I want to total the fgm and fga, in this example. Each year's stats are stored in separate tables.

jatar_k

3:17 pm on Sep 8, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that makes more sense, I assume this returns stats for a single player. I added something a little extra. Since the three portions are almost exactly the same except for the table they are querying I had a foreach loop run through an array of years. This should all work given a single player stats return.

$storedyrs = array("200001","200102","200203"); 
$tfgm = 0;
$tfga = 0;
foreach ($storedyrs as $actyr) {
$getdata = "select * from " . $storedyrs . "playerstats where playerid = $activeplayer";
$result = mysql_query($getdata);
while ($row = mysql_fetch_array($result)) {
extract($row);
$fgpct = $fgm / $fga;
$f_fgpct = sprintf("%.3f", $fgpct);
echo "$year $fgm-$fga ($f_fgpct)<br>";
$tfgm += $fgm;
$tfga += $fga;
}
}