Jump to content

HELP!! Error in my code??


shmeeg

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/213935-help-error-in-my-code/
Share on other sites

<?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'");
}
?>

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());
}
?>

  • 3 weeks later...

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'");
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.