基本上我编写了一个网站,允许用户发布体育赛事。然后成员可以评价每个事件。评分系统为10分制:0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0。写5星级评分系统的最佳方法是什么?
有四类:
组织,物有所值,设施,娱乐因素
我的值存储在使用float
领域的MySQL数据库。
我已经使用了以下虚拟数据来测试系统。
- org = 2,v4m = 4.5,fac = 4,fun = 2.5;
- org = 3.5,v4m = 4,fac = 4.5,fun = 1.5;
- org = 1,v4m = 5,fac = 4.5,fun = 2.5;
当根据上面的内容显示事件评分时,我希望每个类别都轮到上面提到的5星级评分系统。
到目前为止,我有下面的代码,除了四舍五入之外,它似乎工作正常。
例如:目前三家机构评级的平均值为2.1666666666667
,我想将其平均为2.0
。如果平均数为4.6666666666667
,我希望它的值为4.5
。
但我该怎么做?
继承人到目前为止我的代码:
// Count how many ratings this event has
if($ratingCount = $event->getEventRatingCount($event_id)){
// Get ratings for this event
$eventRating = $event->getEventRatings($event_id);
$organisation = 0;
$valueForMoney = 0;
$facilities = 0;
$funFactor = 0;
$overall = 0;
foreach($eventRating AS $rating){
$organisation = ($organisation + $rating['organisation']/$ratingCount);
$valueForMoney = ($ValueForMoney + $rating['value_for_money']/$ratingCount);
$facilities = ($facilities + $rating['facilities']/$ratingCount);
$funFactor = ($funFactor + $rating['fun_factor']/$ratingCount);
}
echo '<br />';
echo $organisation . '<br />';
echo $valueForMoney . '<br />';
echo $facilities . '<br />';
echo $funFactor . '<br />';
echo $overall = ($organisation + $valueForMoney + $facilities + $funFactor)/$ratingCount);
}else{
// no ratings for this event
}
随着虚拟数据我目前得到这些值:
2.1666666666667
1.6666666666667
4.3333333333333
2.1666666666667
3
如果我有50分的评分,这还能工作吗? – crm
取决于你使用哪个50。如果评分是0.5,1,1.5,...,25:是的,它会的。 – Joni
对不起我的数学是不好的^^;所以只要最高的选择是5并且使用这两种方法(你的和我的)就应该没问题? – crm