Search code examples
phprating-systemrich-snippets

How to transform with php likes and dislikes in a 5star rating system?


Let's say i have a database entry like this:

video_id | likes | dislikes

 654         5          12

What i want to do is to transform the number of likes and dislikes into a 5 stars rating for google rich snippets.

Do you have any ideas? Some code?


Solution

  • To get a rating between 1 and 5, you could do the following:

    $rating = 1 + 4 * $likes / ($likes + $dislikes);
    

    Remember to take special care of the case where there are no votes, though.