How to calculate average on star rating system?

后端 未结 2 1542
野趣味
野趣味 2021-01-03 03:48

I am developing a star rating system with 1-5 stars. In my database I am saving them like this:

$stars_1 = 1;
$stars_2 = 6;
$stars_3 = 3;
$stars_4 = 11;
$sta         


        
相关标签:
2条回答
  • 2021-01-03 04:02

    You need to multiply the number of stars with the actual rating. Like

    $points_stars_2 = $stars_2 * 2  
    ...  
    $points_stars_5 = $stars_5 * 5 
    

    And then you add them all to one variable like in your code, and then divide it by $total_votes.

    Regards

    0 讨论(0)
  • 2021-01-03 04:09

    Needs to be like this:

    ($stars_1 + $stars_2 * 2 + $stars_3 * 3 + $stars_4 * 4 + $stars_5 * 5) / $total_votes;
    
    0 讨论(0)
提交回复
热议问题