Speedy84 Posted November 2, 2008 Share Posted November 2, 2008 I want to set up a 5 star rating system on my site. But I need some help with the calculations.. all I can come up with at this point is create a column in the db called "points" and one called "totaltvotes" and then do something like "points/totalvotes".. but i'm positive that this is wrong =) Link to comment https://forums.phpfreaks.com/topic/131114-5-star-rating-calculation-help/ Share on other sites More sharing options...
Mchl Posted November 2, 2008 Share Posted November 2, 2008 Why you're so positive about it being wrong? It will give you an average for given item. Is it not what you want? Link to comment https://forums.phpfreaks.com/topic/131114-5-star-rating-calculation-help/#findComment-680743 Share on other sites More sharing options...
bobbinsbro Posted November 2, 2008 Share Posted November 2, 2008 what you probably want to find the weighted average. something like this: (1*(total 1 start votes) + 2*(total 2 star votes) + ... + 5*(total 5 star votes))/(total votes) if you want a round number, use the round() function. Link to comment https://forums.phpfreaks.com/topic/131114-5-star-rating-calculation-help/#findComment-680744 Share on other sites More sharing options...
Mchl Posted November 2, 2008 Share Posted November 2, 2008 That's same as having one star being worth one point, and five stars being worth five points... Why overcomplicate? Link to comment https://forums.phpfreaks.com/topic/131114-5-star-rating-calculation-help/#findComment-680745 Share on other sites More sharing options...
Speedy84 Posted November 2, 2008 Author Share Posted November 2, 2008 hm... well i might try, but what should i do? something like this? $points = 5; $totalvotes = 1; $rating = $points / $totalvotes; if (($rating >= 1) && ($rating <= 49)) { show_one_star; } elseif (($rating >= 50) && ($rating <= 99)) { show_two_stars; } Link to comment https://forums.phpfreaks.com/topic/131114-5-star-rating-calculation-help/#findComment-680746 Share on other sites More sharing options...
Andy-H Posted November 2, 2008 Share Posted November 2, 2008 http://gr0w.com/articles/code/php_5_star_rating_system_using_hreview/ Link to comment https://forums.phpfreaks.com/topic/131114-5-star-rating-calculation-help/#findComment-680748 Share on other sites More sharing options...
Mchl Posted November 2, 2008 Share Posted November 2, 2008 No. $starsToShow = round($points/$totalvotes); Link to comment https://forums.phpfreaks.com/topic/131114-5-star-rating-calculation-help/#findComment-680749 Share on other sites More sharing options...
laffin Posted November 2, 2008 Share Posted November 2, 2008 Careful with that, ya might get a divide by zero $rating = round(($points && $totalvotes)?($points / $totalvotes):0); is all u need, cuz ya only have 1 - 5 as a scale so dividing the total points by total votes will still give u a range of 1 - 5 using the round function well makes shure yer at a whole number switch($ratiing) { case 1: // 1 star break; . . default: // no stars } or use if/ifelse/else structure Link to comment https://forums.phpfreaks.com/topic/131114-5-star-rating-calculation-help/#findComment-680753 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.