1-5星评级 - 如何计算平均分

时间:2013-08-29 20:36:34

标签: php math vote

我有4套星级评级小工具,可以保存到我的数据库,每颗星的值(1-5),用户可以使用这些4星组给其他用户评分。

类似的东西:

is user x a jealous person? * * // (=2)
is user x a happy person? * * * * // (=4) 
is user x a clever person? * * * * * // (=5)
is user x a humble person? * * * * * // (=5)

所以投票后我得到了这样的记录:

*mysql sample row data

-------------------------------------------------------------------
ID  from_user_id  to_user_id rate_value
-------------------------------------------------------------------
1    2234         123         2    
2    2234         123         4   
3    2234         123         5   
4    2234         123         5   
-------------------------------------------------------------------

运行某种查询。 结果如下

    id=1
    from_user_id = 2234
    to_user_id = 123
    a1=2
    a2=4
    a3=5
    a4=5

用户可以获得的最佳分数是20。

在用户有两个或更多分数行后,我想为他保存一个十进制分数,从0到10,比如说:7.1。

我该怎么做?对不起,我不能数学。

3 个答案:

答案 0 :(得分:2)

如果您想要 1-10 比例,

  

你所要做的就是将总分除以2。   因此,如果他们有20个中的15个,则相当于10个中的7.5个。>因为 15/20 = 0.75和7.5 / 10

答案 1 :(得分:1)

好吧,首先我们需要确定你的得分为10分。如果我们将它从星星上取下来,它们只会达到5,所以我们会加倍它。

//Initialize Variable
$score = 0;

//Use for loop to go through $row['a#'] values
for( $x = 1; $x < 5; ++$x )
{
    //Add $row['a#'] after multiplying
    $score += ( $row['a'.$x] * 2 );
}
//divide by 4 (number of scores) and round to first decimal spot
$score = round( $score/4, 1 );

echo $score; //Outputs 8  ( 4+8+10+10 = 32 => 32/4 = 8 )

答案 2 :(得分:-1)

Algorithm:   Calculate_Average_Star_Rating ( )
1.  for each (product exixts in table_product HDB) do
2.  for each (Authenticated user rate for respective product in table_rating HDB) do
3.      Read corresponding weight from ratings column in table_rating HDB
        Calculate Weighted_Mean = ∑ ( weight * number of users at that weight )/
                                             Total number of users
4.  end for 
5.  Store Weighted_Mean in corresponding rows avg_rating column in table_product HDB
6. end for      
相关问题