Php 在五星评级系统中将评级转换为百分比
我在我的一个项目中有一个五星评级系统。我想把每颗星转换成百分比值 这是我的数据库Php 在五星评级系统中将评级转换为百分比,php,math,formulas,Php,Math,Formulas,我在我的一个项目中有一个五星评级系统。我想把每颗星转换成百分比值 这是我的数据库 star1 star2 star3 star4 star5 tot_stars average 计算我用来获得评级 $cal = ($star1*1 + $star2*2 + $star3*3 + $star4*4 + $star5*5)/$total_stars; $avg = number_format($cal, 2); 我想知道的是,每颗星星的百分比是多少 示例:Yelp.com在他们的网
star1
star2
star3
star4
star5
tot_stars
average
计算我用来获得评级
$cal = ($star1*1 + $star2*2 + $star3*3 + $star4*4 + $star5*5)/$total_stars;
$avg = number_format($cal, 2);
我想知道的是,每颗星星的百分比是多少
示例:Yelp.com在他们的网站上做这件事,我建议你在数据库中做数学运算,让它返回一个浮点值。这样,您可以轻松地以“百分比”样式显示星星 例如:
SELECT (CAST(
(star1*1) + (star2*2) + (star3)*3 + (star4*4) + (star5*5)
as float)/15) as percentageStars
作为显示器的一个示例:
- 五星:80-100
- 四星:60-80
- 三星:40-60
- 双星:20-40
- 1星:0-20
%1
)来获取剩余值。例如,这个rest值可以乘以100得到css的百分比填充因子
公式:
4.8%1=0.8*100=80
希望这能对您有所帮助?好吧,我建议您在数据库中进行计算,让它返回一个浮点值。这样,您可以轻松地以“百分比”样式显示星星 例如:
SELECT (CAST(
(star1*1) + (star2*2) + (star3)*3 + (star4*4) + (star5*5)
as float)/15) as percentageStars
作为显示器的一个示例:
- 五星:80-100
- 四星:60-80
- 三星:40-60
- 双星:20-40
- 1星:0-20
%1
)来获取剩余值。例如,这个rest值可以乘以100得到css的百分比填充因子
公式:
4.8%1=0.8*100=80
希望这能对你有所帮助?如果你的目标是获得(例如)1星级的评级百分比,那么只需将
star1
除以评级数量(我猜这是tot_stars
,尽管这个名字似乎暗示着其他情况)。这会让你得到一个比例;乘以100得到一个百分比
例如,让数据库进行计算:
SELECT star1, star1 * 100 / tot_stars AS percent1,
star2, star2 * 100 / tot_stars AS percent2,
star3, star3 * 100 / tot_stars AS percent3,
star4, star4 * 100 / tot_stars AS percent4,
star5, star5 * 100 / tot_stars AS percent5
FROM starsTable
或者在PHP中执行此操作:
<?php
// Example data
$star1 = 1;
$star2 = 5;
$star3 = 7;
$star4 = 10;
$star5 = 8;
$tot_stars = $star1 + $star2 + $star3 + $star4 + $star5;
for ($i=1;$i<=5;++$i) {
$var = "star$i";
$count = $$var;
$percent = $count * 100 / $tot_stars;
for ($j=1;$j<=5;++$j) {
echo $j <= $i ? "☆ " : " ";
}
printf("\t%2d (%5.2f%%)\n", $count, $percent,2);
}
?>
如果您的目标是获得(例如)1星级的评分百分比,那么只需将
star1
除以评分数(我猜这是tot_stars
,尽管名称似乎暗示了其他含义)。这会让你得到一个比例;乘以100得到一个百分比
例如,让数据库进行计算:
SELECT star1, star1 * 100 / tot_stars AS percent1,
star2, star2 * 100 / tot_stars AS percent2,
star3, star3 * 100 / tot_stars AS percent3,
star4, star4 * 100 / tot_stars AS percent4,
star5, star5 * 100 / tot_stars AS percent5
FROM starsTable
或者在PHP中执行此操作:
<?php
// Example data
$star1 = 1;
$star2 = 5;
$star3 = 7;
$star4 = 10;
$star5 = 8;
$tot_stars = $star1 + $star2 + $star3 + $star4 + $star5;
for ($i=1;$i<=5;++$i) {
$var = "star$i";
$count = $$var;
$percent = $count * 100 / $tot_stars;
for ($j=1;$j<=5;++$j) {
echo $j <= $i ? "☆ " : " ";
}
printf("\t%2d (%5.2f%%)\n", $count, $percent,2);
}
?>
我提出了一个解决方案,这是一个函数,它得到一个数组,其中有星星作为值。我们计算总值,然后逐个计算百分比。我们返回百分比。每个百分比将对应于输入中具有相同索引的星
function getPercentages($inputValues) {
$totalValues = 0;
foreach ($inputValues as $inputValue) {
$totalValues += $inputValue;
}
$outputValues = array();
foreach ($inputValues as $key => $inputValue) {
$outputValues[$key] = 100 * $inputValue / $totalValues;
}
return $outputValues;
}
我提出了一个解决方案,这是一个函数,它得到一个数组,其中有星星作为值。我们计算总值,然后逐个计算百分比。我们返回百分比。每个百分比将对应于输入中具有相同索引的星
function getPercentages($inputValues) {
$totalValues = 0;
foreach ($inputValues as $inputValue) {
$totalValues += $inputValue;
}
$outputValues = array();
foreach ($inputValues as $key => $inputValue) {
$outputValues[$key] = 100 * $inputValue / $totalValues;
}
return $outputValues;
}
我不太确定我是否明白你的问题。你只是想要一个百分比值来给星星上色,对吗?等等,什么?为什么5颗星不只是每个都值20%?1颗星不是20%,2颗星不是40等等吗?@Ionic不,我想把每颗星的评级作为一个百分比。例如:如果有1 x 5星评级和1 x 4星评级,则每颗星将有50%的评级。我希望你现在能理解我的问题。因此,
star1
是一星级评级的数量。好的,我不太确定我是否明白你的问题。你只是想要一个百分比值来给星星上色,对吗?等等,什么?为什么5颗星不只是每个都值20%?1颗星不是20%,2颗星不是40等等吗?@Ionic不,我想把每颗星的评级作为一个百分比。例如:如果有1 x 5星评级和1 x 4星评级,则每颗星将有50%的评级。我希望你现在能理解我的问题。因此,star1
是一星级评级的数量。好的,很有魅力。非常感谢,非常有魅力。非常感谢你。