I don't know much about making this all fit together but hopefully someone does. This is supposobly a really good code that took a lot of studying and brainy type of patience. Anyways if you need more info let me know...
<?php
$dataYear = "data" . $_GET['year'] . ".php";
include $dataYear;
if (count($season) < 1 || count($ranks) < 1) { echo "No Data"; exit; }
function getError(array $ranking, array $week) {
$error = 0;
foreach ($week as $game) {
$loser = $game[2];
$winner = $game[0];
$loseRank = $ranking[$loser];
$winRank = $ranking[$winner];
$margin = $game[3] - $game[4];
# echo("#$winRank $winner $game[3] $game[1] #$loseRank $loser $game[4] <br/>");
if ($margin > 0 && $winRank < $loseRank) continue;
$error += ($winRank - $loseRank) + ($margin / 7);
if ($game[1] == '@') $error += 1;
}
# echo ("error = $error<br/>");
return $error;
}
function doMaxError($thiserror, $rank, $week, $maxerror) {
if ($maxerror[4][0] > $thiserror) return $maxerror;
if ($maxerror[3][0] > $thiserror) {
$maxerror[4][0] = $thiserror; $maxerror[4][1] = $rank; $maxerror[4][2] = $week;
return $maxerror;
}
if ($maxerror[2][0] > $thiserror) {
$maxerror[4][0] = $maxerror[3][0]; $maxerror[4][1] = $maxerror[3][1]; $maxerror[4][2] = $maxerror[3][2];
$maxerror[3][0] = $thiserror; $maxerror[3][1] = $rank; $maxerror[3][2] = $week;
return $maxerror;
}
if ($maxerror[1][0] > $thiserror) {
$maxerror[4][0] = $maxerror[3][0]; $maxerror[4][1] = $maxerror[3][1]; $maxerror[4][2] = $maxerror[3][2];
$maxerror[3][0] = $maxerror[2][0]; $maxerror[3][1] = $maxerror[2][1]; $maxerror[3][2] = $maxerror[2][2];
$maxerror[2][0] = $thiserror; $maxerror[2][1] = $rank; $maxerror[2][2] = $week;
return $maxerror;
}
if ($maxerror[0][0] > $thiserror) {
$maxerror[4][0] = $maxerror[3][0]; $maxerror[4][1] = $maxerror[3][1]; $maxerror[4][2] = $maxerror[3][2];
$maxerror[3][0] = $maxerror[2][0]; $maxerror[3][1] = $maxerror[2][1]; $maxerror[3][2] = $maxerror[2][2];
$maxerror[2][0] = $maxerror[1][0]; $maxerror[2][1] = $maxerror[1][1]; $maxerror[2][2] = $maxerror[1][2];
$maxerror[1][0] = $thiserror; $maxerror[1][1] = $rank; $maxerror[1][2] = $week;
return $maxerror;
}
$maxerror[4][0] = $maxerror[3][0]; $maxerror[4][1] = $maxerror[3][1]; $maxerror[4][2] = $maxerror[3][2];
$maxerror[3][0] = $maxerror[2][0]; $maxerror[3][1] = $maxerror[2][1]; $maxerror[3][2] = $maxerror[2][2];
$maxerror[2][0] = $maxerror[1][0]; $maxerror[2][1] = $maxerror[1][1]; $maxerror[2][2] = $maxerror[1][2];
$maxerror[1][0] = $maxerror[0][0]; $maxerror[1][1] = $maxerror[0][1]; $maxerror[1][2] = $maxerror[0][2];
$maxerror[0][0] = $thiserror; $maxerror[0][1] = $rank; $maxerror[0][2] = $week;
return $maxerror;
}
$maxerror = array(array("0","",""),array("0","",""),array("0","",""),array("0","",""),array("0","",""));
foreach ($ranks as $rank) {
$error = 0;
for ($week =4; $week < 19; $week++) {
$thiserror = 0;
$mult = 40;
$totalmult = 50;
for($j = $week; $j < 19 && $j < $week + 8; $j++) {
$thiserror += $mult * getError($rank[$week], $season[$j]);
$mult = $mult / 1.5;
}
$thiserror = $thiserror / $totalmult;
$error += $thiserror;
$maxerror = doMaxError($thiserror,$rank[0],$week, $maxerror);
}
$error = $error / 15;
$results[$rank[0]] = $error;
}
asort($results);
echo "Rank | error per week<br/>";
foreach ($results as $key => $val) {
echo "$key | $val<br/>";
}
echo "<br/><br/><br/> Max error =<br/>";
foreach ($maxerror as $maxer) {print "$maxer[0], $maxer[1], week $maxer[2]<br/>";}
?>