I'm working on my rating system, I actually have it setup already, what I'm trying to do is potentially setup some iterations that runs the code over and over for every team until their ratings normalize.
I have a current MSVB code that does the same thing...
Here is my code:
<?
require ( "dbconnex.php" );
/* SELECT ALL GAMES */
$games_rs = $conn->Execute ( "SELECT * FROM game ORDER BY game_date DESC" ) or die ( $conn->ErrorMsg() );
/* START LOOP */
while ( ! $games_rs->EOF ) {
/* GET TEAM ONE INFORMATION */
$team1_rs = $conn->Execute ( "SELECT * FROM team WHERE teamID = " . $games_rs->Fields("team1") . " LIMIT 1" ) or die ( $conn->ErrorMsg() );
$team1_rated = $team1_rs->Fields("teamID");
$team1_win = $team1_rs->Fields("team_won");
$team1_loss = $team1_rs->Fields("team_lost");
$team1_tie = $team1_rs->Fields("team_tie");
$team1_gp = $team1_rs->Fields("team_gp");
$team1_xr = $team1_rs->Fields("team_xrating");
/* GET TEAM TWO INFORMATION */
$team2_rs = $conn->Execute ( "SELECT * FROM team WHERE teamID = " . $games_rs->Fields("team2") . " LIMIT 1" ) or die ( $conn->ErrorMsg() );
$team2_rated = $team2_rs->Fields("teamID");
$team2_win = $team2_rs->Fields("team_won");
$team2_loss = $team2_rs->Fields("team_lost");
$team2_tie = $team2_rs->Fields("team_tie");
$team2_gp = $team2_rs->Fields("team_gp");
$team2_xr = $team2_rs->Fields("team_xrating");
/* START TEAM ONE CALCULATIONS */
/* IF TEAM 1 WON */
if ( $games_rs->Fields("team1_score") > $games_rs->Fields("team2_score") ) {
$team1_gp_new = $team1_gp + 1;
$team1_win_new = $team1_win + 1;
$team1_loss_new = $team1_loss;
$team_tie_new = $team1_tie;
$team1_xr_diff = $team2_xr - $team1_xr;
$team1_rating_new = $team1_xr + 21 + $team1_xr_diff / 25;
}
/* IF TEAM 1 LOST */
if ( $games_rs->Fields("team1_score") < $games_rs->Fields("team2_score") ) {
$team1_gp_new = $team1_gp + 1;
$team1_loss_new = $team1_loss + 1;
$team1_win_new = $team1_win;
$team1_tie_new = $team1_tie;
$team1_xr_diff = $team2_xr - $team1_xr;
$team1_rating_new = $team1_xr - 21 + $team1_xr_diff / 25;
}
/* IF TEAM 1 TIED */
if ( $games_rs->Fields("team1_score") == $games_rs->Fields("team2_score") ) {
$team1_gp_new = $team1_gp + 1;
$team1_tie_new = $team1_tie + 1;
$team1_win_new = $team1_win;
$team1_loss_new = $team1_loss;
$team1_xr_diff = $team2_xr - $team1_xr;
$team1_rating_new = $team1_xr + 0 + $team1_xr_diff / 25;
}
/* START TEAM TWO CALCULATIONS */
/* IF TEAM 2 WON */
if ( $games_rs->Fields("team2_score") > $games_rs->Fields("team1_score") ) {
$team2_gp_new = $team2_gp + 1;
$team2_win_new = $team2_win + 1;
$team2_loss_new = $team2_loss;
$team2_tie_new = $team2_tie;
$team2_xr_diff = $team1_xr - $team2_xr;
$team2_rating_new = $team2_xr + 21 + $team2_xr_diff / 25;
}
/* IF TEAM 2 LOST */
if ( $games_rs->Fields("team2_score") < $games_rs->Fields("team1_score") ) {
$team2_gp_new = $team2_gp + 1;
$team2_loss_new = $team2_loss + 1;
$team2_tie_new = $team2_tie;
$team2_win_new = $team2_win;
$team2_xr_diff = $team1_xr - $team2_xr;
$team2_rating_new = $team2_xr - 21 + $team2_xr_diff / 25;
}
/* IF TEAM 2 TIED */
if ( $games_rs->Fields("team2_score") == $games_rs->Fields("team1_score") ) {
$team2_gp_new = $team2_gp + 1;
$team2_tie_new = $team2_tie + 1;
$team2_win_new = $team2_win;
$team2_loss_new = $team2_loss;
$team2_xr_diff = $team1_xr - $team2_xr;
$team2_rating_new = $team2_xr + 0 + $team2_xr_diff / 25;
}
$sql = "UPDATE team SET team_xrating = $team1_rating_new, team_gp = $team1_gp_new, team_won = $team1_win_new, team_lost = $team1_loss_new, team_tie = '$team1_tie_new' WHERE teamID = $team1_rated";
$add_rating_rs = $conn->Execute($sql) or die ( $conn->ErrorMsg() );
$sql = "UPDATE team SET team_xrating = $team2_rating_new, team_gp = $team2_gp_new, team_won = $team2_win_new, team_lost = $team2_loss_new, team_tie = '$team2_tie_new' WHERE teamID = $team2_rated";
$add_rating1_rs = $conn->Execute($sql) or die ( $conn->ErrorMsg() );
$games_rs->MoveNext(); } ?>
Also, I'd to include an IF clause at the beginning of every loop that unless team1 AND team2 are in the teams table it ignores that loop. So if I have teams 1-4 in the teams table and for some reason Team1 plays Team5 (who isn't part of the teams table), that loop is skipped all together.