PHP Code:
<script>
var http_request = false;
function makePOSTRequest(url, parameters) { http_request = false;
if (window.XMLHttpRequest) {http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); }
} else if (window.ActiveXObject) {
try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
}
}
if (!http_request) { alert('Cannot create XMLHTTP instance'); return false; }
document.getElementById('myspan').innerHTML = "Loading...";
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Charset", "windows-1251");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
result = http_request.responseText;
document.getElementById('myspan').innerHTML = result;
} else { alert('Please try again later.'); }
}
}
function rate(article_id, vote){ makePOSTRequest('star_rating.php', 'article_id=' +article_id+'&vote='+vote); }
if (document.getElementById) { window.onload = myUnobtrusiveBehavior; }
function myUnobtrusiveBehavior() {
var percentstyle="width:<?echo @number_format($current_rating/$count,2)*20 ?> px;";
var content ="<div class=\"rating\" id=\"rating\"> <ul class=\"star-rating\"> <li class=\"current-rating\" style=\""+percentstyle+"\" >Current rating</li> <li><a id=\"rate1\" class=\"one-star\">1</a></li> <li><a id=\"rate2\" class=\"two-stars\">2</a></li> <li><a id=\"rate3\" class=\"three-stars\">3</a></li> <li><a id=\"rate4\" class=\"four-stars\">4</a></li> <li><a id=\"rate5\" class=\"five-stars\">5</a></li></ul>";
/*
var myspan = document.createElement("div");
myspan.setAttribute("id", "myspan");
myspan.innerHTML = content;
document.body.appendChild(myspan);
*/
document.getElementById("myspan").innerHTML=content;
if (document.getElementById("rating")) {
var article_id=document.getElementById("item").value;
document.getElementById("rate1").onclick = function () {rate(article_id,'1'); return false;}
document.getElementById("rate2").onclick = function () {rate(article_id,'2'); return false;}
document.getElementById("rate3").onclick = function () {rate(article_id,'3'); return false;}
document.getElementById("rate4").onclick = function () {rate(article_id,'4'); return false;}
document.getElementById("rate5").onclick = function () {rate(article_id,'5'); return false;}
}
}
</script>
<style>
.star-rating,
.star-rating a:hover,
.star-rating a:active,
.star-rating a:focus,
.star-rating .current-rating{
background: url(star_rating.gif) left -1000px repeat-x;
}
.star-rating{
position:relative;
width:125px;
height:25px;
overflow:hidden;
list-style:none;
margin:0;
padding:0;
background-position: left top;
}
.star-rating li{
display: inline;
}
.star-rating a,
.star-rating .current-rating{
position:absolute;
top:0;
left:0;
text-indent:-1000em;
height:25px;
line-height:25px;
outline:none;
overflow:hidden;
border: none;
}
.star-rating a:hover,
.star-rating a:active,
.star-rating a:focus{
background-position: left bottom;
}
.star-rating a.one-star{
width:20%;
z-index:6;
}
.star-rating a.two-stars{
width:40%;
z-index:5;
}
.star-rating a.three-stars{
width:60%;
z-index:4;
}
.star-rating a.four-stars{
width:80%;
z-index:3;
}
.star-rating a.five-stars{
width:100%;
z-index:2;
}
.star-rating .current-rating{
z-index:1;
background-position: left center;
}
/* for an inline rater */
.inline-rating{
display:-moz-inline-block;
display:-moz-inline-box;
display:inline-block;
vertical-align: middle;
}
/* smaller star */
.small-star{
width:50px;
height:10px;
}
.small-star,
.small-star a:hover,
.small-star a:active,
.small-star a:focus,
.small-star .current-rating{
background-image: url(star_small.gif);
line-height: 10px;
height: 10px;
}
</style>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'poliferico';
$dbname = 'menu';
$tableName="ratings";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname);
$rating_posted=$_POST['vote'];//the actual user vote
$id=$_POST['article_id'];//passed article_id Line 158
if (empty($id)) {$id=$article_id;} // line 159
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM $tableName WHERE id='$id' ")or die(" Error: ".mysql_error());
$numbers=mysql_fetch_assoc($query);
$checkIP=unserialize($numbers['used_ips']);
$count=$numbers['total_votes'];//how many votes total
$current_rating=$numbers['total_value'];//total number of rating added together and stored
$sum=$rating_posted+$current_rating;// add together the current vote value and the total vote value
$tense=($count==1) ? "vote" : "votes";//plural form votes/vote
$voted=mysql_fetch_assoc(mysql_query("SELECT * FROM $tableName WHERE used_ips LIKE '%".$_SERVER['REMOTE_ADDR']."%' AND id='$id' ")); //This variable searches through the previous ip addresses that have voted and returns true or false
//when already voted
if($voted){
echo "<ul class=\"star-rating\">
<li class=\"current-rating\" style=\"width:". @number_format($current_rating/$count,2)*20 ."%;\"></li>
</ul>
Rating: <strong>".@number_format($current_rating/$count,2)."</strong> ( $count $tense )
<br /><span style=\"color:red;\">You've already voted.</span><br />";
}
else{
//if not voted do the actual voting
if(isset($rating_posted)){
//open initial voting row if necessary
if (mysql_num_rows($query) == 0) { $sql = "INSERT INTO $tableName (id, total_votes, total_value, used_ips) VALUES ('$id', '0', '0', '')"; $result = mysql_query($sql) or die("err");}
//increment votes, check ips & add/update vote to table
if($sum==0){ $added=0;} else{ $added=$count+1;}
if(is_array($checkIP)){array_push($checkIP,$_SERVER['REMOTE_ADDR']);}else{ $checkIP=array($_SERVER['REMOTE_ADDR']);}
$insert=serialize($checkIP);
mysql_query("UPDATE $tableName SET total_votes='$added', total_value='$sum',used_ips='$insert' WHERE id='$id'") or die("Error");
$response = "<div class=\"rating\">Rate: <strong>".@number_format($sum/$added,2)."</strong> <br /> <span style=\"color:red;\">Thank you for your vote cast!</span></div>";
echo iconv("windows-1251", "UTF-8", $response);
exit;
}
//if not voting just show the current rating
else { echo "<div id=\"myspan\"></div>";}
}//end if
?>
Bookmarks