Any help would be appreciated on this... I am doing this locally on WinXP using IIS4, PHP, and mySQL (latest production revs) and don't have a host that's php enabled yet.
I am trying to do some calculations from a table, and am having trouble figuring it out.
(I am fairly new to php and mySQL, and can do the basics, but am stumbling with the calculations)
OK, here's the scenario... 2 tables, bowlers and scores and their SQL is below:
CREATE TABLE bowlers (
bowlerID int(11) NOT NULL auto_increment,
abcID int(11) default NULL,
lastname varchar(25) NOT NULL default '',
firstname varchar(25) NOT NULL default '',
dateadded date default NULL,
PRIMARY KEY (bowlerID),
UNIQUE KEY abcID (abcID)
) TYPE=MyISAM COMMENT='This table holds the specific bowler information.' AUTO_INCREMENT=11 ;
CREATE TABLE scores (
scoreID int(11) NOT NULL auto_increment,
bowlerID int(11) default NULL,
datebowled date default NULL,
game1 int(3) default NULL,
game2 int(3) default NULL,
game3 int(3) default NULL,
series int(11) default NULL,
PRIMARY KEY (scoreID)
) TYPE=MyISAM AUTO_INCREMENT=21 ;
Now, the bowlerID from the bowlers table is relational to the bowlerID field in the scores table.
What I want to do is to run a query that totals game1, game2 and game3 from the scores table to create a total pinfall for a given week. I then want to add up all of the total pinfalls for each week for a given bowler (I will explain this better in a moment). Lastly, I want to count the total number of games bowled during that time frame and divide my total pinfall by the total numbre of games bowled to find an average.
In other words, say bowler#1 bowls 200, 200, 200 in week 1. I want to add that up to get the 600 for that week.
Now, say he bowls 200, 200, 200 for the next 4 weeks. I want to add all of those up to get 3000. (or 600 for each week of the 5 week span)
I want to add up a count to determine that 15 games were bowled.
I want to calculate the average (3000/15) to get 200.
I have to be able to determine if a score value is NULL, so I can factor that in (just in case someone only bowls 2 of 3 games), to the calculation doesn't count that as one of the games in the final calculation.
In other words, it would be 2800/14 instead.
I figured out this much (to count all of the games bowled) but I am sure this can be cleaned up:
$game1=mysql_query("select game1 from scores where bowlerID = $bid AND game1 is NOT NULL");
$game2=mysql_query("select game2 from scores where bowlerID = $bid AND game2 is NOT NULL");
$game3=mysql_query("select game3 from scores where bowlerID = $bid AND game3 is NOT NULL");
$countg1=mysql_num_rows($game1);
$countg2=mysql_num_rows($game2);
$countg3=mysql_num_rows($game3);
$totalcount=$countg1+$countg2+$countg3;
I figured out this much (to total weekly pinfall):
$scoretotals=mysql_query("SELECT game1, game2, game3, (game1+game2+game3) as totalscore from scores where bowlerID=$bid");
The problem is that my weekly pinfall query calculates each row, but I can't figure out how to store that variable, and then add up all of the weekly pinfalls.
I am thinking I have to use an array somehow, but can't figure it out. Any help would be greatly appreciated.