-5

I have one product and i want to calculate 5 star rating on the basic like and dislike

for example product have

  • like = 200 // that means 200 user like that product
  • dislike =10 // that means 10 user dislike that particular product
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Chauhan
  • 2,441
  • 8
  • 37
  • 46
  • like/(like + dislike) would get you some percentage... where each 20% would be a complete star...? – nightshade Jun 20 '14 at 03:29
  • How to calculate star rating on the basis of like count and dislike count ? – Chauhan Jun 20 '14 at 03:30
  • 2
    `200/210 = 0.9523` then do `0.9523 * 5 ` and that is your star rating `4.761`. HOWEVER, it's not accurate info and may be misleading since you don't know if all 200 people who liked before, would have gave 5 stars. Maybe most of them would of only gave 4 if they had the option? – CRABOLO Jun 20 '14 at 03:34
  • 2
    Q: “How to calculate star rating?” A: Math. – Giacomo1968 Jun 20 '14 at 03:37
  • 1
    +1 @JakeGould I tend to think that I was glad I paid attention in math class ;-) well, most of the time. – Funk Forty Niner Jun 20 '14 at 03:39
  • I wouldn't use a linear percentage * stars mapping. That's hardly weighting in the negative votes or resulting in a representative distribution. (Which you can't unless you have inspected a realistic amount of data points.) – mario Jun 20 '14 at 03:44
  • Amazed at the results found on Google (after spending 30 seconds on it) using "5 star rating php". – Funk Forty Niner Jun 20 '14 at 03:54
  • 1
    I hate stars. Is shows on a scale of 1 (?!) to 5 (?!) the number of fake reviews plus randomness. No - a score system where upvote adds one and downvote subtracts one is better. – bjb568 Jun 20 '14 at 04:24

2 Answers2

4

Below is javascript calculation just to get the idea. You can convert it to whatever language you want.

var like = 200;
var dislike = 10
var total = like + dislike ; 

percentOfLikes    = 5 * like / total;
percentOfDislikes = 5 * dislike / total;
console.log(percentOfLikes);
// 4.761904761904762 4.8 stars
console.log(percentOfDislikes);
// 0.23809523809523808 0.2 stars
nyzm
  • 2,787
  • 3
  • 24
  • 30
0

Here's a simple PHP function:

<?php
    function calculateStarRating($likes, $dislikes){ 
        $maxNumberOfStars = 5; // Define the maximum number of stars possible.
        $totalRating = $likes + $dislikes; // Calculate the total number of ratings.
        $likePercentageStars = ($likes / $totalRating) * $maxNumberOfStars;
        return $likePercentageStars;
    }
?>

To call the function:

<?php
    $likeCount = 190; 
    $dislikeCount =10; 
    $calculatedRating = calculateStarRating($likeCount, $dislikeCount); 
?>

To use the calculated value in Javascript:

<script>
    var rating = <?php echo $calculatedRating; ?>;
    alert(rating); // For testing purposes.
</script>

These would get you started.

TribalChief
  • 797
  • 5
  • 11
  • Also, do have a look at this [link](http://stackoverflow.com/questions/1987524/turn-a-number-into-star-rating-display-using-jquery-and-css?rq=1) – TribalChief Jun 20 '14 at 03:55