Twister1004 Posted July 9, 2009 Share Posted July 9, 2009 Hey everyone! I'm the loser who is trying to make a 5 star rating thingy. Yeah I know I know. Well, either I thought of this as being too simple and wrote it like that, or I just totally didn't think it though. Which I think it is both cases! So, If I may ask for some help, It would be greatly appreciated! Objective: Figure out a proper way to write this script to where it won't fail. As in it will never decrease to 0 all the time. Problem: When I add a NEW rating to the script, or to the variable, IT will allways decrease to 0. Even if it's all 5's. My Code will be in 2 pieces. I will provide any other information that is needed. Thank you to everyone in advance! FYI: I don't know how to use OOP. My functions page: function getblogRating($id){ $sql = mysql_query("SELECT `rating` FROM `blog` WHERE `id` = '$id' LIMIT 1"); $getBlog = mysql_fetch_array($sql); return $getBlog['rating']; } function postblogRating($id, $rating){ if($rating == 0 || $rating == NULL){ return "No value entered"; } $sql = mysql_query("SELECT * FROM `blog` WHERE `id` = '$id' LIMIT 1"); $rate = mysql_fetch_array($sql); $voters = $rate['voted'] + 1; $combine = ($rate['totalrate'] + $rating) / $voters; echo $combine; $totalRate = "$rate[totalrate] + $rating"; $update1 = mysql_query("UPDATE `blog` SET `rating` = '".$combine."', `voted` = '".$voters."', `totalrate` = '".$totalRate."' WHERE `id` = '".$id."'"); if($update1){ $update2 = mysql_query("UPDATE `peoplerating` SET `blogid` = '$id', `user` = '".$_SESSION['name']."'"); if($update2){ return "Your rating has been successfully posted!"; } else{ echo "error"; } } else{ echo "error"; } } function peopleRating($blogid, $name){ if($name == "" || $name == NULL){ return 2; } $Rated = mysql_query("SELECT * FROM `peoplerating` WHERE `blogid` = '$blogid' AND `user` = '$name' LIMIT 1"); if(mysql_num_rows($Rated) > 0){ return 1; } else{ return 0; } } Page with the displaying information. PART 1 @$checkUser = peopleRating(mysql_real_escape_string($_GET['blog']), $_SESSION['name']); if($checkUser == 0){ echo "<div style='position:absolute; top:40px; text-align:right; margin-left:137px;'> <form method='post' action='' <input type=\"radio\" name=\"thisRating\" value=\"1\" /> 1 <input type=\"radio\" name=\"thisRating\" value=\"2\" /> 2 <input type=\"radio\" name=\"thisRating\" value=\"3\" /> 3 <input type=\"radio\" name=\"thisRating\" value=\"4\" /> 4 <input type=\"radio\" name=\"thisRating\" value=\"5\" /> 5 <input type=\"submit\" name=\"vote\" value=\"Vote!\" /> </form> </div>"; } elseif($checkUser == 2){ echo "<div style='position:absolute; top:40px; text-align:right; margin-left:300px;'>Please sign in</div>"; } elseif($checkUser == 1){ echo "<div style='position:absolute; top:40px; text-align:right; margin-left:215px;'>You have voted here before</div>"; } Part 2 if(isset($_POST['vote'])){ postblogRating(mysql_real_escape_string($_GET['blog']), $_POST['thisRating']); //echo "<meta http-equiv='refresh' content='0; url='''/>"; } Link to comment https://forums.phpfreaks.com/topic/165307-solved-a-5-star-rating-thingy/ Share on other sites More sharing options...
Daniel0 Posted July 9, 2009 Share Posted July 9, 2009 Have a table like this: votes vote_id (primary key) user_id (foreign key referencing the voter to prevent double votes) blog_id (foreign key referencing the thing that's voted on) rating (an integer in the range of 1 to 5) So when the person with user id 10 votes on the blog post with id 31 and wants to give it 4 stars you do this (after checking they didn't already vote): INSERT INTO votes (user_id, blog_id, rating) VALUES (10, 31, 4); When you want to get the score and the number of voters for blog post id 31 you do like this: SELECT SUM(v.rating)/COUNT(*) AS score, COUNT(*) AS num_votes FROM votes AS v INNER JOIN blog AS b ON b.id = v.blog_id WHERE b.id = 31 GROUP BY b.id; Link to comment https://forums.phpfreaks.com/topic/165307-solved-a-5-star-rating-thingy/#findComment-871818 Share on other sites More sharing options...
Twister1004 Posted July 9, 2009 Author Share Posted July 9, 2009 Thank you very much, Daniel!! It worked, but I was struggling about how to call it out. xD. but I ended up just doing this. function getblogRating($id){ $sql = mysql_query("SELECT SUM(rating)/COUNT(*) AS score, COUNT(*) AS num_votes FROM blograting AS v INNER JOIN blog AS b ON b.id = v.blogid WHERE b.id = '$id' GROUP BY b.id"); $getRating = mysql_fetch_array($sql); return $getRating[0]; } I really appreciate your work. -hugs- Link to comment https://forums.phpfreaks.com/topic/165307-solved-a-5-star-rating-thingy/#findComment-872157 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.