Jump to content

Need help with rating system


supermerc

Recommended Posts

Hey Im trying to make my rating system work, as of now it doesnt enter the vote in the database, for example if someone submit a new peice of art and I go to rate it and i click 4 stars, then I got in the database and it will have entered the id of the rating we just did, and the ID of the art we just rated but in the rating and number of votes put 0 I dont know why.

 

Here is my code:

 

<?php
include'config.php';
$tableName="ratings";
$rating_posted=$_GET['vote'];//pased variable by the the stars value
$id=(INT)$_GET['member_id'];
$sql = "SELECT total_votes, total_value, used_ips FROM $tableName WHERE which_id = $id";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $numbers = mysql_fetch_assoc($result);
    $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
  } else {
    echo "No results found";
  }
} else {
  echo "Query failed<br />$sql<br />". mysql_error();
}
$voted=@mysql_fetch_assoc(@mysql_query("SELECT title FROM $tableName WHERE used_ips LIKE '%".$_SERVER['REMOTE_ADDR']."%' AND id='$id' ")); //Pattern match ip:suggested by Bramus! http://www.bram.us/ - this variable searches through the previous ip address that have voted and returns true or false

if($voted){
echo "<div class=\"rating\">".
"<ul class=\"star-rating\">".
"<li class=\"current-rating\" style=\"width:". @number_format($current_rating/$count,2)*20 ."px;\">Current rating.</li>".
     "<li class=\"one-star\">1</li>".
     "<li class=\"two-stars\" >2</li>".
     "<li class=\"three-stars\">3</li>".
     "<li class=\"four-stars\">4</li>".
     "<li class=\"five-stars\">5</li>".
"</ul>".
"<p>Rating: <strong>".@number_format($current_rating/$count,2)."</strong> {".$count." ".$tense." cast} <br />You have previously voted.</p></div>";//show the current value of the vote with the current numbers
}else{

if(isset($_GET['vote'])){

if($sum==0){
$added=0;//checking to see if the first vote has been tallied
}else{
$added=$count+1;//increment the current number of votes
}

if(is_array($checkIP)){
array_push($checkIP,$_SERVER['REMOTE_ADDR']);//if it is an array i.e. already has entries the push in another value
}else{
$checkIP=array($_SERVER['REMOTE_ADDR']);//for the first entry
}

$insert=serialize($checkIP);
$query = mysql_query("select * from ratings where which_id=$id");
$num = mysql_num_rows($query);
if ($num != 1)
{
mysql_query("INSERT INTO ratings(total_votes, total_value, used_ips, which_id) VALUES ('{$added}','{$sum}','{$insert}', '{$id}')");

}
else
{
mysql_query("UPDATE $tableName SET total_votes='".$added."', total_value='".$sum."', used_ips='".$insert."' WHERE which_id='".$_GET['id']."'");
}

echo 	"<div class=\"rating\"><p>Rating: <strong>".@number_format($sum/$added,2)."</strong> {".$added." ".$tense." cast} <span>Thank you for your vote!</span></p></div>";//show the updated value of the vote
}else{
?>
<link href="star_rating.css" rel="stylesheet" type="text/css" />


<div class="rating">
<p>How clear was this tutorial?</p>
<ul class="star-rating">
<li class="current-rating" style="width:<?php echo @number_format($current_rating/$count,2)*20 ?>px;">Current rating</li>
     <li><a href="<?php echo $_SERVER['PHP_SELF'] . "?" .$_GET['section'] . "member_id=" . (INT)$_GET['member_id'] . "&vote=1";?>" title="Rate this 1 star out of 5" class="one-star">1</a></li>
     <li><a href="<?php echo $_SERVER['PHP_SELF'] . "?" .$_GET['section'] . "member_id=" . (INT)$_GET['member_id'] . "&vote=1";?>" title="Rate this 2 stars out of 5" class="two-stars" >2</a></li>
     <li><a href="<?php echo $_SERVER['PHP_SELF'] . "?" .$_GET['section'] . "member_id=" . (INT)$_GET['member_id'] . "&vote=1";?>" title="Rate this 3 stars out of 5" class="three-stars" >3</a></li>
     <li><a href="<?php echo $_SERVER['PHP_SELF'] . "?" .$_GET['section'] . "member_id=" . (INT)$_GET['member_id'] . "&vote=1";?>" title="Rate this 4 stars out of 5" class="four-stars" >4</a></li>
     <li><a href="<?php echo $_SERVER['PHP_SELF'] . "?" .$_GET['section'] . "member_id=" . (INT)$_GET['member_id'] . "&vote=1";?>" title="Rate this 5 stars out of 5" class="five-stars" >5</a></li>
</ul>
<?php
echo	"<p>Rating: <strong>".@number_format($sum/$count,2)."</strong> {".$count." ".$tense." cast}</p></div>";//show the current updated value of the vote
}	// end isset get vote	
}	//end voted true, false
?>

Link to comment
https://forums.phpfreaks.com/topic/47025-need-help-with-rating-system/
Share on other sites

if ($result = mysql_query($sql)) {

  if (mysql_num_rows($result)) {

 

unless I am reading this code wrong - these 2 if statements will always return false because you never defined $result.

 

 

Additionally, your code is redundant - and I don't know if thats a good thing or a bad thing. i.e. you define $tableName for no apparent reason at the top of your code, instead of just using 'ratings' in your query like you do lower down in your script, or like your first statement,

 

if ($result = mysql_query($sql)) {

is the same as

if ($result) {

if you can post some of the main components of your form, it might provide a bit more insight. Just going on an assumption that you are posting form data to this page, the following would be the source of your error:

 

you are setting the following :

$rating_posted=$_GET['vote'];

 

if you are dealing with posted data this would be where

you need to make a small, but important change:

$rating_posted=$_POST['vote'];

 

if you are really using the <form method="GET"> you will want to make these changes anyways. Passing form data via the url is a good way to all access to your db.

 

also, yzerman's call outs should be reviewed. You should always keep your

code consistent throughout your page/app. Clean code benefits you and anyone who needs to review it.

 

if the suggestion i mentioned doesn't help, post your form here for the community to have a better idea where the data is coming from.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.