alexville Posted December 10, 2008 Share Posted December 10, 2008 Hey guys, I want to make a rating system for my game site, and what I have in mind is using a php system where a user and submit a number from 1 to 10 and then it is inserted into a "ratings" table with a reference to the game's page id. But what do I do to show the average rating once the data is in the table? Do I have to get all the ratings for a single game and average them out some how to get a rating? Can someone help? Link to comment https://forums.phpfreaks.com/topic/136419-averaging-out-ratings/ Share on other sites More sharing options...
phpSensei Posted December 10, 2008 Share Posted December 10, 2008 Do you want to get the overall averaging rating, or just for a single page? Link to comment https://forums.phpfreaks.com/topic/136419-averaging-out-ratings/#findComment-711899 Share on other sites More sharing options...
alexville Posted December 10, 2008 Author Share Posted December 10, 2008 Do you want to get the overall averaging rating, or just for a single page? I want to get the average rating for 1 game, not all of them. Link to comment https://forums.phpfreaks.com/topic/136419-averaging-out-ratings/#findComment-711904 Share on other sites More sharing options...
redarrow Posted December 10, 2008 Share Posted December 10, 2008 looks good... <?php $query = "SELECT field_name, AVG(field_name) FROM what_ever GROUP BY field_name"; $result = mysql_query($query) or die(mysql_error()); ?> Link to comment https://forums.phpfreaks.com/topic/136419-averaging-out-ratings/#findComment-711916 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 $user_rated/$rated_times = $value; if(user_rated < $existing_rating){ $new_rating = $rating - $value; } else { $new_rating = $rating + $value; } //update db by changing rating and adding one to the rated_times field Link to comment https://forums.phpfreaks.com/topic/136419-averaging-out-ratings/#findComment-711917 Share on other sites More sharing options...
phpSensei Posted December 10, 2008 Share Posted December 10, 2008 Grab all the queries for the page id given (WHERE page_id = '$id'), then divide the ratings by the number of ratings to get the average Link to comment https://forums.phpfreaks.com/topic/136419-averaging-out-ratings/#findComment-711926 Share on other sites More sharing options...
redarrow Posted December 10, 2008 Share Posted December 10, 2008 this might work This should do it i think.... <?php $query = "SELECT AVG(number) FROM what_ever where id='$id'"; $result = mysql_query($query) or die(mysql_error()); ?> Link to comment https://forums.phpfreaks.com/topic/136419-averaging-out-ratings/#findComment-711930 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.