【问题标题】:How to calculate average on star rating system?如何计算星级系统的平均值?
【发布时间】:2012-03-19 10:12:35
【问题描述】:

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

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

$total_votes = 43

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

我现在就这样做,但我不工作(结果似乎错误):

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

【问题讨论】:

  • $stars_1 和 $stars_5 之间有什么区别,如果最后你平均它们? $stars_1 和 $stars_5 扮演同样的角色。

标签: php vote


【解决方案1】:

需要是这样的:

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

【讨论】:

    【解决方案2】:

    您需要将星数乘以实际评分。喜欢

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

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

    问候

    【讨论】:

      猜你喜欢
      • 2015-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      相关资源
      最近更新 更多