基本上我正在编写一个允许用户发布体育赛事的网站。然后会员可以评估每个活动。评级系统是10分制:0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0。
有四类:
组织,物有所值,设施,趣味因素
我使用float
字段将值存储在mysql数据库中。
我使用了以下虚拟数据来测试系统。
根据上述情况显示事件评级时,我希望每个类别都围绕上述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
答案 0 :(得分:2)
您可以将等级乘以2,将其映射到1..10范围。然后围绕它,除以2。
$overall = round(2*$overall)/2;
这会将4.666映射到4.5和2.1666到2.评级3.888将向上舍入为4.如果您想要始终向下舍入,请使用floor
而不是round
。
答案 1 :(得分:0)
将每个评级存储到数据库的表中;例如 表
ID |评分
1 | 4.5
2 | 3.5
3 | 3.0
现在创建一个查询,该查询将获取该表中的所有值并创建所有结果的平均值
你基本上只是将所有结果存储在某处,然后计算平均值
然后根据平均值创建一个开关或if语句来显示你想要的内容。
对不起,数字/评级没有出现在我预期的方式......