ok, ive made some serious headway... it updates the database and i know it has the ability to update the rating correctly... for some reason its not and i cant quite figure out what im missing... here is the javascript:
Code:
// JavaScript Document
var ratequest
function rateIt(id, rating){
ratequest=GetXmlHttpObject();
addId = "add" + id;
if (ratequest==null){
alert ("Your browser does not support AJAX!");
return;
}
var url="ajax/rating.php";
url=url+"?id=" + id;
url=url+"&rating=" + rating;
ratequest.onreadystatechange=stateChanged;
ratequest.open("GET",url,true);
ratequest.send(null);
return newRate;
}
function stateChanged(){
if (ratequest.readyState==4){
newRate = ratequest.responseText;
}
}
function GetXmlHttpObject(){
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject){
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
$$('.rate').each(function(element,i){
element.addEvent('mouseup', function(){
var newPosition = rateIt((element.getParent().get('id').replace('rate','')), parseInt(element.title));
var myStyles = ['nostar', 'onestar', 'twostar', 'threestar', 'fourstar', 'fivestar'];
myStyles.each(function(myStyle){
if(element.getParent().hasClass(myStyle)){
element.getParent().removeClass(myStyle);
}
});
myStyles.each(function(myStyle, index){
if(index == newPosition){
element.getParent().toggleClass(myStyle);
}
});
});
});
and the php file is connects to:
PHP Code:
<?php
session_start();
include("../lib/login.php");
login();
$username = $_SESSION['user'];
$listingid = $_GET['id'];
$rating = (int)$_GET['rating'];
$query = "SELECT COUNT(rating) FROM ratings WHERE listingid = '$listingid' AND userid = '$username'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$totrecords = $row[0];
if($totrecords == 0){
$query = "INSERT INTO ratings (listingid, userid, rating) VALUES ('$listingid', '$username', '$rating')";
$result = mysql_query($query) or die(mysql_error());
}
else{
$query = "UPDATE ratings SET listingid = '$listingid', userid = '$username', rating = '$rating' WHERE listingid = '$listingid' AND userid = '$username'";
$result = mysql_query($query) or die(mysql_error());
}
$query = "SELECT rating FROM ratings WHERE listingid ='$listingid'";
$result = mysql_query($query);
//$row = mysql_fetch_array($result, MYSQL_ASSOC);
$allratings = array();
$i=0;
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$allratings[$i] = $row['rating'];
$i++;
}
$num_of_ratings = count($allratings);
$rating = ceil(array_sum($allratings) / $num_of_ratings);
echo $rating;
?>
what happens is the first time its clicked, it will display the image for no starts... then the second time its clicked, it will show what it should have shown the first time... and so on and so forth... anyone see something im not?
Bookmarks