C0nw0nk
C0nw0nk

Reputation: 960

PHP To Calculate AgrregateRating out of 5 stars

So I have the following values of Number of likes on a item and Number of Dislikes on a item i can combine the two figures to get the total number of votes.

$likes = 4300; //number of likes
$dislikes = 8000; //number of dislikes
$total = $likes + $dislikes; //total number of votes by combining the two figures together

But I how can I take these values and create a AggregateRating out of 5 stars for a schema markup I am doing.

Upvotes: 0

Views: 50

Answers (1)

user8034901
user8034901

Reputation:

If a "like" represents a "5-star-rating" and a "dislike" represents a "1-star-rating" you can get the average like so:

$likes = 4300;
$dislikes = 8000;
$average = ($likes*5 + $dislikes*1) / ($likes + $dislikes);

echo $average;

Will output 2.3983739837398

You can use number_format() to limit the decimals:

echo number_format($average,1);

Will output 2.4

Upvotes: 3

Related Questions