我正在尝试以星星和半星的形式创建1到5(包括.5值)的用户评级的直观表示。我已经从MySQL数据库创建了用户评级数组,我已经平均这些值以创建数字形式的平均用户评级。我想做的就是拿这个值..
$averageuserrating
如果该值为“1”,则返回此代码一次
echo '<img src="images/star.png" class="ratingstar" />';
如果$ averageuserrating为1.5,则返回此代码
<img src="images/star.png" class="ratingstar" /><img src="images/halfstar.png" class="ratingstar" />
等等......如果没有10个嵌套的if语句,有没有办法做到这一点?
答案 0 :(得分:3)
可以通过以下方式完成:
$fullStar = '<img src="images/star.png" class="ratingstar" />';
$halfStar = '<img src="images/halfstar.png" class="ratingstar" />';
$rating = 1.5;
while($rating > 0){
if($rating >= 1){
echo $fullStar;
$rating = $rating - 1;
continue;
}
if($rating >= 0.5){
echo $halfStar;
$rating = $rating - 0.5;
}
}
另一方面,这不是显示评级系统的最佳方式。
答案 1 :(得分:1)
for($i = 1; $i <= floor($averageuserrating); $i++)
echo '<img src="images/star.png" class="ratingstar" />';
if($averageuserrating > floor($averageuserrating))
echo '<img src="images/halfstar.png" class="ratingstar" />';
注意:如果平均值不仅增加0.5,而且例如你可以得到1.4: 将第二个条件更改为:
if($averageuserrating >= (floor($averageuserrating) + 0.5))
答案 2 :(得分:1)
只需使用str_repeat
函数和一点数学。
$full_star = '<img src="images/star.png" class="ratingstar" />';
$half_star = '<img src="images/halfstar.png" class="ratingstar" />';
echo str_repeat( $full_star, floor($averageuserrating) );
if(! is_int($averageuserrating) ){
echo $half_star;
}
我认为它会正常工作。
答案 3 :(得分:0)
您可以使用switch语句。
switch($value){
case "1";
echo '<img src="images/star1.png" class="ratingstar" />';
break;
case "1";
echo '<img src="images/star2.png" class="ratingstar" />';
break;
so on and so forth..
}
答案 4 :(得分:0)
以下是可能解决方案的代码:
$numWholeStars = (int)$averageuserrating;
$numHalfStars = $averageuserrating - (int)$averageuserrating;
for($i = 0; $i < $numWholeStars; $i++) {
echo '<img src="images/star.png" class="ratingstar" />';
}
if($numHalfStars != 0) {
echo '<img src="images/halfstar.png" class="ratingstar" />';
}
答案 5 :(得分:0)
听起来你需要重新调整基础代码的因素a)收集所有评级b)显示评级应显示的星数。首先看php math - modulus
get_user_ratings($user="bob")
{
15 - the user ratings should/could be in 1 place (int)users.total_ratings
// lets say there are 15 records similar to the one below
jim, 1, bob
...
mary, 1.5, bob
// calculate the average of all votes
...
...
/* If your rating system offers the chance to rate a user 1.5 stars
use modulus to check if the rating needs half stars */
$stars = "";
if( $averageuserrating % 1 == 0)
{
for(
i=0;
i<$averageuserrating-1;
$i++;$stars .= '<img src="images/star.png" class="ratingstar" />';
)
return $stars;
}
else
{
for(
i=0;
i<$averageuserrating-1;
$i++;$stars .='<img src="images/star.png" class="ratingstar" />'
)
$stars .= '<img src="images/halfstar.png" class="ratingstar" />';
return $stars;
}
}
答案 6 :(得分:0)
$fullstar = '<img src="images/star.png" class="ratingstar" />';
$halfstar = '<img src="images/halfstar.png" class="ratingstar" />';
$value=2.5;//some rating
$number = number_format ( $value, 1 );
$fs = floor ( $number ); //full stars
$hs=(($value*10)%10)/5; //half stars
while($fs>0)
{
echo $fullstar;
$fs=$fs-1;
}
if($hs==1)
echo $halfstar;