Location: PHPKode > scripts > Ja2BU > vote.php
<?php session_start();
if(@$_SESSION['INC_USER_ID'] == false) {
	echo "<div style=\"\">&nbsp;<font style=\"font-size:12px;color: #fff;\">Login</font>&nbsp;";
	die();
}
$id = $_POST['id'];
if(isset($_COOKIE['crated'.$id])) {
	echo "<div style='color:#fff'>...</div>";
	die();
}else{
@setcookie("crated".$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 reviews where revid = ?',array($conn->addq($id)));
	if(!$arecordSet)
		print $conn->ErrorMsg();
	else
		while(!$arecordSet->EOF) {
			$votes[0] = $arecordSet->fields['revrating'];
			$votes[1] = $arecordSet->fields['revratings'];
			$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 == 'cvote_up') {
	$votes_up = $cur_votes[0] + 1;
	$q = $conn->Prepare('UPDATE reviews SET revrating = ? WHERE revid = ?');
	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> &nbsp;<font style=\"font-size:14px;font-family:'trebuchet ms';color: #6B6D70;\"><b>Votes</b></font>&nbsp;";
	}
} elseif($action == 'cvote_down') //voting down
{
	$votes_down = $cur_votes[1] + 1;
	$q = $conn->Prepare('UPDATE reviews SET revratings = ? WHERE revid = ?');
	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> &nbsp;<font style=\"font-size:14px;font-family:'trebuchet ms';color: #6B6D70;\"><b>Votes</b></font>&nbsp;";
	}
}
?>
Return current item: Ja2BU