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
I have one product and i want to calculate 5 star rating on the basic like and dislike
for example product have
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
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.