Go Back
1)
// header("Location: error.php");
// Output here? Why not!
?>
Vote Submitted (redirecting...)
$votes = mysql_result($result, 0, 'votes');
// Extend vote string as needed
while (strlen($votes) <= $id)
$votes .= "-";
// If there's no vote registered
if ($votes{$id} == "-") {
vote($id, $v); // issue vote, scale of 1-10
$v = ($v == 10) ? 0 : $v; // switch to 1-digit scale
$votes{$id} = $v;
$query = "UPDATE votes SET votes = '$votes' WHERE ip = '$ip'";
mysql_query($query) or die('Error: '.mysql_error());
echo "
Thank you for voting!
";
} else {
cvote($id, $v, $votes{$id}); // Issue vote, leaving $old in 1-0 format
$v = ($v == 10) ? 0 : $v; // Switch to 1-digit
$votes{$id} = $v;
$query = "UPDATE votes SET votes = '$votes' WHERE ip = '$ip'";
mysql_query($query) or die('Error: '.mysql_query());
echo "
Your vote has been changed.
";
}
echo "You will be redirected to quotes.php in 2 seconds...";
}
function vote($id, $vote) { // $vote is between 1 and 10
$query = "SELECT * FROM quotes WHERE id = '$id'";
$result = mysql_query($query);
$s = mysql_result($result, 0, 'score');
$v = mysql_result($result, 0, 'votes');
$query = "UPDATE quotes SET score = '".(($s*$v + $vote)/($v+1))."', votes = '".($v+1)."' WHERE id = '$id'";
mysql_query($query) or die('Error: '.mysql_error());
}
// If someone wants to change their vote:
function cvote($id, $vote, $old) { // $vote is betw. 1 and 10, but $old is still 1-0
$query = "SELECT * FROM quotes WHERE id = '$id'";
$result = mysql_query($query);
// Prepare for math:
$old = ($old == 0) ? 10 : $old;
$s = mysql_result($result, 0, 'score');
$v = mysql_result($result, 0, 'votes');
$query = "UPDATE quotes SET score = '".(($s*$v + $vote - $old)/($v))."' WHERE id = '$id'";
mysql_query($query) or die('Error: '.mysql_error());
}
?>