This program takes a bunch of NFL player's stats, calculates their passing rates and then displays all of the information. It's mostly completed but I'm having trouble in a few different areas.
$rate = "";
$file = 'purposely took the file location out';
$fp = fopen($file, 'r');
$name = "";
$team = "";
$completions = "";
$attempts = "";
$yards = "";
$touchdowns = "";
$interceptions = "";
function getNflStats($file){
@ $fp = fopen($file, 'r');
if(!$fp){
echo "Could not open the file!<br/>";
exit;
}
$nflStats = array();
while($data = fgetcsv($fp,255,',')){
$name = $data[0];
$team = $data[1];
$completions = $data[2];
$attempts = $data[3];
$yards = $data[4];
$touchdowns = $data[5];
$interceptions = $data[6];
$rate = Calculate($completions, $attempts, $yards, $touchdowns,
$interceptions);
$nflStats[] = array("name"=>$rate);
}
fclose($fp);
return $nflStats;
}
How do I go about changing the values in a sub-array? Within the calculate function I have created an array that holds all of my calculated values. Through lots of attempts, I figured out that each of those values $a,$b,$c, and $d is its own array because of the while loop. I'm trying to check whether any value in any of the sub-arrays is above 2.375 or below 0 and if so, I will then change that value to be 2.375 or 0. I've been working on this for 3 hours but have yet to find a solution on how to run through each of the sub-arrays and change each individual element.
function Calculate($completions, $attempts, $yards, $touchdowns,
$interceptions){
$a = ($completions/$attempts - .3) * 5;
$b = ($yards/$attempts - 3) * .25;
$c = ($touchdowns/$attempts) * 20;
$d = (2.375 - ($interceptions/$attempts *25));
$result = array($a,$b,$c,$d);
echo "$result[0] <br/>";
$size = count($result[0]);
foreach($result as $value){
for($i=0;$i < $size; $i++){
if($result[i] > 2.375){
$result[i] = 2.375;
}
}
}
$passRating = round((($a + $b + $c + $d)/6) * 100, 2);
return $passRating;
}
I'm also having trouble displying the player's name and they're passing rate. I can only ever produce one or the other but not both at the same time. I'm unsure whether its an issue with my array formatting or foreach loop.
function displayStats($nflStats){
asort($nflStats);
echo "<table>";
echo "<thead><tr>
<td><h2>Player</h2></td>
<td></td>
<td><h2>Rating</h2></td>
</tr></thead>";
foreach($nflStats as $stats){
echo "<tr>
<td>{$stats['name']}</td>
<td></td>
<td style='text-align:right'></td>
</tr>";
}
echo "</table>";
}
displayStats($nflStats);
?>
Here is some of the data that I'm reading from the file:
Tom Brady,NE,385,581,4577,32,8
Philip Rivers,LAC,360,575,4515,28,10
Matthew Stafford,DET,371,565,4446,29,10
Drew Brees,NO,386,536,4334,23,8
Ben Roethlisberger,PIT,360,561,4251,28,14
Matt Ryan,ATL,342,529,4095,20,12
Kirk Cousins,WAS,347,540,4093,27,13
Alex Smith,KC,341,505,4042,26,5
Russell Wilson,SEA,339,553,3983,34,11
Jared Goff,LA,296,477,3804,28,7
Blake Bortles,JAX,315,523,3687,21,13
Case Keenum,MIN,325,481,3547,22,7
Jameis Winston,TB,282,442,3504,19,11
Derek Carr,OAK,323,515,3496,22,13
Eli Manning,NYG,352,571,3468,19,13
Dak Prescott,DAL,308,490,3324,22,13
Andy Dalton,CIN,297,496,3320,25,12
Cam Newton,CAR,291,492,3302,22,16
Carson Wentz,PHI,265,440,3296,33,7
Marcus Mariota,TEN,281,453,3232,13,15
Joe Flacco,BAL,352,549,3141,18,13
Jacoby Brissett,IND,276,469,3098,13,7
Josh McCown,NYJ,267,397,2926,18,9
Currently I'm getting a bunch of errors regarding the variable i or just the values show:
sample of values: (trying to get them to not be negative or over ~158)
-12.8
-11.25
27.08
31.25
31.77
35.42
46.65
55.06
55.85
60.5
63.39
65.79
66.39
68.15
69.23
70.6
71.44
71.92
71.92
71.99
72.34
72.47
72.53
73.3
75.61
76.37
76.88
77.5
172.92
206.25
214.58
254.17
268.75
577.08
631.25