I wrote a code that lets the user vote for one out of the two options and the score is calculated using a modified Elo rating system. However, digits to the right of the decimal point are ignored when I vote for either of the options. I added the floatval function but it didn’t help.
$query = "SELECT pic,score,id FROM nfl
JOIN (SELECT r1.random_id
FROM nfl_map AS r1
JOIN (SELECT (RAND() *
(SELECT MAX(row_id)
FROM nfl_map)) AS row_id)
AS r2
WHERE r1.row_id >= r2.row_id
ORDER BY r1.row_id ASC
LIMIT 1) as rows ON (id = random_id)";
$query_2 = "SELECT pic,score,id FROM nfl
JOIN (SELECT r1.random_id
FROM nfl_map AS r1
JOIN (SELECT (RAND() *
(SELECT MAX(row_id)
FROM nfl_map)) AS row_id)
AS r2
WHERE r1.row_id >= r2.row_id
ORDER BY r1.row_id ASC
LIMIT 1) as rows ON (id = random_id)";
$res_2 = $mysqli->query($query_2);
if (!$res_2) die ("Result display failed: " . $mysqli->errno . " - " . $mysqli->error);
$pic_2 = $res_2->fetch_assoc();
$id_2 = $res_2->fetch_assoc();
$score_2 = $res_2->fetch_assoc();
$res_1 = $mysqli->query($query);
if (!$res_1) die ("Result display failed: " . $mysqli->errno . " - " . $mysqli->error);
$pic_1 = $res_1->fetch_assoc();
$id_1 = $res_1->fetch_assoc();
$score_1 = $res_1->fetch_assoc();
$Ra = $score_1;
$Rb = $score_2;
$calc_pow_1 = (($Rb - $Ra) / 400);
$calc_pow_2 = (($Ra - $Rb) / 400);
$float_calc_pow_1 = floatval($calc_pow_1);
$float_calc_pow_2 = floatval($calc_pow_2);
$Ea = 1 / (1 + pow(10,$float_calc_pow_1));
$Eb = 1 / (1 + pow(10,$float_calc_pow_2));
$float_Ea = floatval($Ea);
$float_Eb = floatval($Eb);
// if user votes for picture no. 1
if (isset($_POST['vote_1']) && isset($_POST['id']) && isset($_POST['score']))
{
$id = $_POST['id'];
/* $score = $_POST['score'];
$pic = $_POST['pic']; */
//$mod = 2 * $Eb;
$K = 4;
$float_mod_1 = floatval($K * (1 - $float_Ea));
$query = "UPDATE nfl SET score=?+$float_mod_1 WHERE id=?";
// $score_new = $_POST['score'] + $mod;
$score_new = $_POST['score'] + $float_mod_1;
$stmt = $mysqli->prepare($query);
$stmt->bind_param('di', $_POST['score'], $_POST['id']);
$stmt->execute();
if ($stmt->errno) {
echo "Vote failed: " . $stmt->error();
}
else echo "Vote succeeded! New score of $id_1 is $score_new!
";
$stmt->close();
}
// fetch picture no. 2
//
// query to the database
/* $query_2 = "SELECT pic,score,id FROM nfl
JOIN (SELECT r1.random_id
FROM nfl_map AS r1
JOIN (SELECT (RAND() *
(SELECT MAX(row_id)
FROM nfl_map)) AS row_id)
AS r2
WHERE r1.row_id >= r2.row_id
ORDER BY r1.row_id ASC
LIMIT 1) as rows ON (id = random_id)";
$res_2 = $mysqli->query($query_2);
if (!$res_2) die ("Result display failed: " . $mysqli->errno . " - " . $mysqli->error);
$pic_2 = $res_2->fetch_assoc();
$id_2 = $res_2->fetch_assoc();
$score_2 = $res_2->fetch_assoc(); */
// if user votes for picture no. 2
if (isset($_POST['vote_2']) && isset($_POST['id']) && isset($_POST['score']))
{
$id = $_POST['id'];
/* $score = $_POST['score'];
$pic = $_POST['pic']; */
//$mod = 2 * $Ea;
$K = 4;
$float_mod_2 = floatval($K * (1 - $float_Eb));
$query = "UPDATE nfl SET score=?+$float_mod_2 WHERE id=?";
// $score_new = $_POST['score'] + $mod;
$score_new = $_POST['score'] + $float_mod_2;
/* $query = "UPDATE nfl SET score=?+1 WHERE id=?";
$score_new = $_POST['score'] + $mod; */
$stmt = $mysqli->prepare($query);
$stmt->bind_param('di', $_POST['score'], $_POST['id']);
$stmt->execute();
if ($stmt->errno) {
echo "Vote failed: " . $stmt->error();
}
else echo "Vote succeeded! New score of $id_2 is $score_new!
";
$stmt->close();
}
Here is the full code, if you need to see it: http://snipt.org/vDhj2
The code handling the situation when the user votes for the second option is pretty much the same (except $Ea changes to $Eb etc.). The ‘score’ row’s properties in MySQL are following:
float(8,3) not null
Thanks.
EDIT: I’m not getting the notice anymore.
I think you simply missed a declaration in your code:
I cannot see
$score_2defined anywhere in the code above where you try to assign it’s value to another variable.Warnings are there for a reason – ignoring them is a bad idea as it will inevitably lead to an error in your code. in this case, you simply need to declare your variable before you assign it (even if it is si,ply
$score_2=0;if that is to be the default value, but assign it nonetheless.