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

Php 1-5星级评分等级-如何计算平均分数,php,math,vote,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) 投票后我得到了这样的记录:

我有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


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

如果你想要一个1-10刻度

你所要做的就是把他们的总分除以2。 因此,如果20人中有15人,这相当于10人中有7.5人。>因为15/20=0.75,7.5/10也一样


首先,我们需要确定你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 )
//初始化变量
$score=0;
//使用for循环遍历$row['a#']值
对于($x=1;$x<5;++$x)
{
//相乘后添加$row['a#']
$score+=($row['a.$x]*2);
}
//除以4(分数),四舍五入至小数点后第一位
$score=四舍五入($score/4,1);
echo$score//输出8(4+8+10+10=32=>32/4=8)

显示的合理平均值不是(2+4+5+5)/4=4吗?(对所有值求和并除以值的总数)如果确实需要x/10值,则可以执行(2+4+5+5)/2=8。(将所有值相加,然后除以2,从20到10)
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