如何计算星级评定系统的平均值


How to calculate average on star rating system?

我正在开发一个1-5颗星的星级评定系统。在我的数据库中,我这样保存它们:

$stars_1 = 1;
$stars_2 = 6;
$stars_3 = 3;
$stars_4 = 11;
$stars_5 = 22;
$total_votes = 43

当用户使用例如三星投票时,我将stars_3更新为1,将total_votes更新为1。然后我需要计算平均评分(星级)。

我现在这样做,但我没有工作(结果似乎是错误的):

(($stars_1 + $stars_2 + $stars_3 + $stars_4 + $stars_4) / $total_votes);

需要这样:

($stars_1 + $stars_2 * 2 + $stars_3 * 3 + $stars_4 * 4 + $stars_5 * 5) / $total_votes;

您需要将星星的数量与实际评级相乘。像

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

然后像代码中那样将它们全部添加到一个变量中,然后除以$total_votes

问候