shmeeg Posted September 20, 2010 Share Posted September 20, 2010 I wrote an ELO calculator but somewhere it's going wrong. Not to insult anyones intelligence but the main jist is K*(S-E) where K is the K factor, which I've set to 60.00 S depends on the outcome, 1 for a win, 0.5 for draw, 0 for loss. E is the calculated rank change: E=1/(1+10^((B-A)/400)) where A = team A rank and B = team B rank. So I then went about writing my calculator. Here's what I have: <?php $team1 = mysql_real_escape_string($_POST['team1']); $team2 = mysql_real_escape_string($_POST['team2']); $ra = mysql_query("SELECT rp FROM teamstats WHERE team='$team1'"); $rb = mysql_query("SELECT rp FROM teamstats WHERE team='$team2'"); $pa = mysql_real_escape_string($_POST['team1goals']); $pb = mysql_real_escape_string($_POST['team2goals']); $ea = 1.0 / (1.0 + pow(10.0, ($rb - $ra) / 400.0)); $eb = 1.0 / (1.0 + pow(10.0, ($ra - $rb) / 400.0)); if ($pa > $pb){ $team1rank = mysql_result($ra, $team1); + 60.0 * (1.0 - $ea); $team2rank = mysql_result($rb, $team2); + 60.0 * (0.0 - $eb); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w + 1, l = l, d = d, gf = gf + '$pa', ga = ga + '$pb', rp = rp + '$team1rank' WHERE team='$team1'"); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l + 1, d = d, gf = gf + '$pb', ga = ga + '$pa', rp = rp + '$team2rank' WHERE team='$team2'"); } elseif ($pa < $pb){ $team1rank = mysql_result($ra, $team1); + 60.0 * (0.0 - $ea); $team2rank = mysql_result($rb, $team2); + 60.0 * (1.0 - $eb); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l + 1, d = d, gf = gf + '$pa', ga = ga + '$pb', rp = rp + '$team1rank' WHERE team='$team1'"); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w + 1, l = l, d = d, gf = gf + '$pb', ga = ga + '$pa', rp = rp + '$team2rank' WHERE team='$team2'"); } elseif ($pa == $pb){ $team1rank = mysql_result($ra, $team1); + 60.0 * (0.5 - $ea); $team2rank = mysql_result($rb, $team2); + 60.0 * (0.5 - $eb); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l, d = d + 1, gf = gf + '$pa', ga = ga + '$pb', rp = rp + '$team1rank' WHERE team='$team1'"); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l, d = d + 1, gf = gf + '$pb', ga = ga + '$pa', rp = rp + '$team2rank' WHERE team='$team2'"); } ?> For some reason, if both teams have a rank of 1000.00 and team A beats team B, instead of team A gaining +30.00 and team B losing -30.00, team A's rank changed to 1037.48 and team B's rank to 972.52. I can't find out why. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
shmeeg Posted September 20, 2010 Author Share Posted September 20, 2010 even without the mysql_result()'s which I realise now I don't need it still doesn't work. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 20, 2010 Share Posted September 20, 2010 If you've changed it, please post the updated code. Quote Link to comment Share on other sites More sharing options...
shmeeg Posted September 20, 2010 Author Share Posted September 20, 2010 <?php $team1 = mysql_real_escape_string($_POST['team1']); $team2 = mysql_real_escape_string($_POST['team2']); $ra = mysql_query("SELECT rp FROM teamstats WHERE team='$team1'"); $rb = mysql_query("SELECT rp FROM teamstats WHERE team='$team2'"); $pa = mysql_real_escape_string($_POST['team1goals']); $pb = mysql_real_escape_string($_POST['team2goals']); $ea = 1.0 / (1.0 + pow(10.0, ($rb - $ra) / 400.0)); $eb = 1.0 / (1.0 + pow(10.0, ($ra - $rb) / 400.0)); if ($pa > $pb){ $team1rank = 60.0 * (1.0 - $ea); $team2rank = 60.0 * (0.0 - $eb); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w + 1, l = l, d = d, gf = gf + '$pa', ga = ga + '$pb', rp = rp + '$team1rank' WHERE team='$team1'"); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l + 1, d = d, gf = gf + '$pb', ga = ga + '$pa', rp = rp + '$team2rank' WHERE team='$team2'"); } elseif ($pa < $pb){ $team1rank = 60.0 * (0.0 - $ea); $team2rank = 60.0 * (1.0 - $eb); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l + 1, d = d, gf = gf + '$pa', ga = ga + '$pb', rp = rp + '$team1rank' WHERE team='$team1'"); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w + 1, l = l, d = d, gf = gf + '$pb', ga = ga + '$pa', rp = rp + '$team2rank' WHERE team='$team2'"); } elseif ($pa == $pb){ $team1rank = 60.0 * (0.5 - $ea); $team2rank = 60.0 * (0.5 - $eb); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l, d = d + 1, gf = gf + '$pa', ga = ga + '$pb', rp = rp + '$team1rank' WHERE team='$team1'"); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l, d = d + 1, gf = gf + '$pb', ga = ga + '$pa', rp = rp + '$team2rank' WHERE team='$team2'"); } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 20, 2010 Share Posted September 20, 2010 You've not done anything with the query result. $ra and $rb are resources, not values when you try to use them. You should be developing with error_reporting(E_ALL), and display_errors = On so these types of things are reported. Try this and see if it runs as expected. <?php $team1 = mysql_real_escape_string($_POST['team1']); $query_a = "SELECT rp FROM teamstats WHERE team='$team1'"; $result_a = mysql_query( $query_a ) or die(mysql_error()); $array_a = mysql_fetch_assoc( $result_a ); $ra = $array_a['rp']; $team2 = mysql_real_escape_string($_POST['team2']); $query_b = "SELECT rp FROM teamstats WHERE team='$team2'"; $result_b = mysql_query( $query_b ) or die(mysql_error()); $array_b = mysql_fetch_assoc( $result_b ); $rb = $array_b['rp']; $pa = mysql_real_escape_string($_POST['team1goals']); $pb = mysql_real_escape_string($_POST['team2goals']); $ea = 1.0 / (1.0 + pow(10.0, ($rb - $ra) / 400.0)); $eb = 1.0 / (1.0 + pow(10.0, ($ra - $rb) / 400.0)); if ( $pa > $pb ) { $team1rank = 60.0 * (1.0 - $ea); $team2rank = 60.0 * (0.0 - $eb); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w + 1, l = l, d = d, gf = gf + '$pa', ga = ga + '$pb', rp = rp + '$team1rank' WHERE team='$team1'") or die(mysql_error()); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l + 1, d = d, gf = gf + '$pb', ga = ga + '$pa', rp = rp + '$team2rank' WHERE team='$team2'") or die(mysql_error()); } elseif( $pa < $pb ) { $team1rank = 60.0 * (0.0 - $ea); $team2rank = 60.0 * (1.0 - $eb); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l + 1,d = d, gf = gf + '$pa', ga = ga + '$pb', rp = rp + '$team1rank' WHERE team='$team1'") or die(mysql_error()); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w + 1, l = l, d = d, gf = gf + '$pb', ga = ga + '$pa', rp = rp + '$team2rank' WHERE team='$team2'") or die(mysql_error()); } elseif( $pa == $pb ) { $team1rank = 60.0 * (0.5 - $ea); $team2rank = 60.0 * (0.5 - $eb); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w,l = l, d = d + 1, gf = gf + '$pa', ga = ga + '$pb', rp = rp + '$team1rank' WHERE team='$team1'") or die(mysql_error()); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w,l = l, d = d + 1, gf = gf + '$pb', ga = ga + '$pa', rp = rp + '$team2rank' WHERE team='$team2'") or die(mysql_error()); } ?> Quote Link to comment Share on other sites More sharing options...
Iris2027 Posted October 11, 2010 Share Posted October 11, 2010 Not a serious problem! <?php $team1 = mysql_real_escape_string($_POST['team1']); $team2 = mysql_real_escape_string($_POST['team2']); $ra = mysql_query("SELECT rp FROM teamstats WHERE team='$team1'"); $rb = mysql_query("SELECT rp FROM teamstats WHERE team='$team2'"); $pa = mysql_real_escape_string($_POST['team1goals']); $pb = mysql_real_escape_string($_POST['team2goals']); $ea = 1.0 / (1.0 + pow(10.0, ($rb - $ra) / 400.0)); $eb = 1.0 / (1.0 + pow(10.0, ($ra - $rb) / 400.0)); if ($pa > $pb){ $team1rank = 60.0 * (1.0 - $ea); $team2rank = 60.0 * (0.0 - $eb); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w + 1, l = l, d = d, gf = gf + '$pa', ga = ga + '$pb', rp = rp + '$team1rank' WHERE team='$team1'"); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l + 1, d = d, gf = gf + '$pb', ga = ga + '$pa', rp = rp + '$team2rank' WHERE team='$team2'"); } elseif ($pa < $pb){ $team1rank = 60.0 * (0.0 - $ea); $team2rank = 60.0 * (1.0 - $eb); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l + 1, d = d, gf = gf + '$pa', ga = ga + '$pb', rp = rp + '$team1rank' WHERE team='$team1'"); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w + 1, l = l, d = d, gf = gf + '$pb', ga = ga + '$pa', rp = rp + '$team2rank' WHERE team='$team2'"); } elseif ($pa == $pb){ $team1rank = 60.0 * (0.5 - $ea); $team2rank = 60.0 * (0.5 - $eb); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l, d = d + 1, gf = gf + '$pa', ga = ga + '$pb', rp = rp + '$team1rank' WHERE team='$team1'"); mysql_query("UPDATE teamstats SET gp = gp + 1, w = w, l = l, d = d + 1, gf = gf + '$pb', ga = ga + '$pa', rp = rp + '$team2rank' WHERE team='$team2'"); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.