<?php session_start();
if(@$_SESSION['INC_USER_ID'] == false) {
echo "<div style=\"\"> <font style=\"font-size:12px;color: #fff;\">Login</font> ";
die();
}
$id = $_POST['id'];
if(isset($_COOKIE['rated'.$id])) {
echo "<div style='color:#fff'>...</div>";
die();
}else{
@setcookie("rated".$id,$id,time() + 60 * 60 * 24 * 365);
}
include ("settings.php");
//taken from http://www.technabled.com/2009/02/reddit-style-voting-with-php-mysql-and.html
/**
* getAllVotes()
*
* @param mixed $id
* @return
*/
function getAllVotes($id) {
$votes = array();
global $conn;
$arecordSet = &$conn->Execute('SELECT * FROM article where blogid = ?',array($conn->addq($id)));
if(!$arecordSet)
print $conn->ErrorMsg();
else
while(!$arecordSet->EOF) {
$votes[0] = $arecordSet->fields['total_rating'];
$votes[1] = $arecordSet->fields['total_ratings'];
$arecordSet->MoveNext();
}
return $votes;
}
/**
* getEffectiveVotes()
*
* @param mixed $id
* @return
*/
function getEffectiveVotes($id) {
$votes = getAllVotes($id);
$effectiveVote = $votes[0] - $votes[1];
return $effectiveVote;
}
$id = $_POST['id'];
$action = $_POST['action'];
$cur_votes = getAllVotes($id);
if($action == 'vote_up') {
$votes_up = $cur_votes[0] + 1;
$q = $conn->Prepare('UPDATE article SET total_rating = ? WHERE blogid = ?');
if($conn->Execute($q,array($conn->addq($votes_up),$conn->addq($id))) === false) {
print '<br /><div id="error">error inserting[1]: '.$conn->ErrorMsg().'</div><br />';
} else {
$effectiveVote = getEffectiveVotes($id);
echo "<div><font style=\"color: #fff;font-size: 15px;font-weight:bold;\">".$effectiveVote.
"</font> <font style=\"font-size:14px;font-family:'trebuchet ms';color: #6B6D70;\"><b>Votes</b></font> ";
}
} elseif($action == 'vote_down') //voting down
{
$votes_down = $cur_votes[1] + 1;
$q = $conn->Prepare('UPDATE article SET total_ratings = ? WHERE blogid = ?');
if($conn->Execute($q,array($conn->addq($votes_down),$conn->addq($id))) === false) {
print '<br /><div id="error">error inserting[1]: '.$conn->ErrorMsg().'</div><br />';
} else {
$effectiveVote = getEffectiveVotes($id);
echo "<div><font style=\"color: #fff;font-size: 15px;font-weight:bold;\">".$effectiveVote.
"</font> <font style=\"font-size:14px;font-family:'trebuchet ms';color: #6B6D70;\"><b>Votes</b></font> ";
}
}
?>