PHP的最佳星级评级解决方案

时间:2012-05-12 10:03:34

标签: php

我想知道编写以下PHP代码的最佳和最有效的方法是什么?

    if ($av == 1) echo '/images/1-star.png';
    if ($av > 1 && < 2) echo '/images/1-half-star.png';
    if ($av == 2) echo '/images/2-star.png';
    if ($av > 2 && < 3) echo '/images/2-half-star.png';

遵循同样的模式,最多5星。

7 个答案:

答案 0 :(得分:5)

就这样使用:

$n = is_int($av) ? $av : floor($av) + 0.5;
echo '/images/'.$n.'-star.png';

enter image description here

每行剪切图像并将其命名为“1-star.png”,“1.5-star.png”,“2-star.png”,“2.5-star.png”,“3-start.png”, “3.5-star.png”等等...

答案 1 :(得分:2)

我猜开关案例最清晰,易于定制

switch (true) {
  case  $av == 1 : 
  echo '/images/1-star.png';
  break;

  case  $av > 1 && $av < 2  : 
  echo '/images/1-half-star.png';
  break;

  case  $av == 2 : 
  echo '/images/2-star.png';
  break;

  case  $av > 2 && $av < 3  : 
  echo '/images/2-half-star.png';
  break;

  /***handle any other cases ****/ 
  default:
  echo '/images/0-star.png';
  break;
}

谢谢Dcoder,他指出了我的好主意

答案 2 :(得分:2)

这可能不是最有效的方式。但我发现它干净的代码。 检查一下。

function renderStarRating($rating,$maxRating=5) {
    $fullStar = "<li><i class = 'fa fa-star'></i></li>";
    $halfStar = "<li><i class = 'fa fa-star-half-full'></i></li>";
    $emptyStar = "<li><i class = 'fa fa-star-o'></i></li>";
    $rating = $rating <= $maxRating?$rating:$maxRating;

    $fullStarCount = (int)$rating;
    $halfStarCount = ceil($rating)-$fullStarCount;
    $emptyStarCount = $maxRating -$fullStarCount-$halfStarCount;

    $html = str_repeat($fullStar,$fullStarCount);
    $html .= str_repeat($halfStar,$halfStarCount);
    $html .= str_repeat($emptyStar,$emptyStarCount);
    $html = '<ul>'.$html.'</ul>';
    return $html;
}

答案 3 :(得分:1)

只有4张图片,一个完整的星星,一半,四分之一和四分之三。

将整个星标打印为abs($av)的次数,然后根据$av - floor($av)的向上/向下舍入值打印另一个星号。

这就是我为一个网站所做的,但他们不想只打印一半,也想要三分之一和四分之一的明星。但逻辑是一样的。

刚刚在线编写代码测试此代码:

 $av = 3.5;

for ($i = 1; $i <= floor($av); $i++) {
    echo "<img src=http://www.leedshospitalalert.org.uk/images/star_shape.gif>";
}

$av2 = $av - floor($av);

if ($av2 > 0.2 && $av2 < 0.8) {
    echo "<img src=http://icdn.pro/images/en/h/a/half-star-icone-6015-48.png>";
} 
elseif ($av2 > 0.7) 
{
    echo "<img src=http://www.leedshospitalalert.org.uk/images/star_shape.gif>";
}

答案 4 :(得分:0)

您可以计算以下数量:

$i = 2 * floor($av - 1) + (floor($av) == $av ? 0 : 1);

这将为您的值生成数字012,...然后您可以使用它们索引到文件名数组。

答案 5 :(得分:0)

你应该在每个标签中使用三元运算符而不是单选按钮

<span class="rating">
                                <input type="radio" id="star5" name="rate<?php echo $cnt; ?>" value="5" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '5') ? 'checked' : '' ?>/>
                                <label class = "full" for="star5" title="5 stars"></label>
                                <input type="radio" id="star4half" name="rate<?php echo $cnt; ?>" value="4.5" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '4.5') ? 'checked' : '' ?>/>
                                <label class="half" for="star4half" title="4.5 stars"></label>
                                <input type="radio" id="star4" name="rate<?php echo $cnt; ?>" value="4" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '4') ? 'checked' : '' ?>/>
                                <label class = "full" for="star4" title="4 stars"></label>
                                <input type="radio" id="star3half" name="rate<?php echo $cnt; ?>" value="3.5" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '3.5') ? 'checked' : '' ?>/>
                                <label class="half" for="star3half" title="3.5 stars"></label>
                                <input type="radio" id="star3" name="rate<?php echo $cnt; ?>" value="3" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '3') ? 'checked' : '' ?>/>
                                <label class="full" for="star3" title="3 stars"></label>
                                <input type="radio" id="star2half" name="rate<?php echo $cnt; ?>" value="2.5" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '2.5') ? 'checked' : '' ?>/>
                                <label class="half" for="star2half" title="2.5 stars"></label>
                                <input type="radio" id="star2" name="rate<?php echo $cnt; ?>" value="2" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '2') ? 'checked' : '' ?>/>
                                <label class = "full" for="star2" title="2 stars"></label>
                                <input type="radio" id="star1half" name="rate<?php echo $cnt; ?>" value="1.5" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '1.5') ? 'checked' : '' ?>/>
                                <label class="half" for="star1half" title="1.5 stars"></label>
                                <input type="radio" id="star1" name="rate<?php echo $cnt; ?>" value="1" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '1') ? 'checked' : '' ?>/>
                                <label class = "full" for="star1" title="1 star"></label>
                                <input type="radio" id="starhalf" name="rate<?php echo $cnt; ?>" value="0.5" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '0.5') ? 'checked' : '' ?>/>
                                <label class="half" for="starhalf" title="0.5 stars"></label>
                            </span>

答案 6 :(得分:-1)

使用if

if ($av==1){
    echo '/images/1-star.png';
} elseif($av > 1 && < 2){
    echo '/images/1-half-star.png';
} elseif ($av==2){
    echo '.....';
}                    // so far so forth

刚刚测试过。切换此操作可能会产生不良后果。