Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP最佳星级解决方案_Php - Fatal编程技术网

PHP最佳星级解决方案

PHP最佳星级解决方案,php,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

我想知道编写以下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';
if($av==1)echo'/images/1-star.png';
如果($av>1&<2)echo'/images/1-half-star.png';
如果($av==2)echo'/images/2-star.png';
如果($av>2&<3)echo'/images/2-half-star.png';
按照相同的模式最多5颗星。

只需这样使用:

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


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

您可以计算以下数量:

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

这将产生数字
0
1
2
。。。对于您的值,您可以使用它来索引到一个文件名数组中。

我想switch case应该是最清晰的,并且易于自定义

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;
}
开关(真){
案例$av==1:
echo'/images/1-star.png';
打破
案例$av>1和$av<2:
echo'/images/1-half-star.png';
打破
案例$av==2:
echo'/images/2-star.png';
打破
案例$av>2和$av<3:
echo'/images/2-half-star.png';
打破
/***处理任何其他案件****/
违约:
echo'/images/0-star.png';
打破
}

谢谢Dcoder,他给我指出了使用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
if($av==1){
echo'/images/1-star.png';
}elseif($av>1&<2){
echo'/images/1-half-star.png';
}elseif($av==2){
回音“…”;
}//迄今为止

刚刚测试。在这种情况下切换可能会产生不理想的结果

只有4张图像,一张全明星、一张半明星、一张四分之一明星和一张四分之三明星

打印满星的次数与
abs($av)
的次数相同,然后根据
$av-floor($av)
的上/下舍入值打印另一个满星

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

刚刚在writecode online上测试了此代码:

 $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>";
}
$av=3.5;
对于($i=1;$i 0.2&&$av2<0.8){
回声“;
} 
elseif($av2>0.7)
{
回声“;
}

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

<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>


这可能不是最有效的方法。但我发现它是干净的代码。 看看这个

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;
}
函数renderStarRating($rating,$maxrrating=5){
$fullStar=“
  • ”; $halfStar=“
  • ”; $emptyStar=“
  • ”;
    $rating=$rating一个基本问题,我在这里看到,条件不是唯一的。因此,所有条件都将被计算。如果只是
    如果
    如果
    ,则使用
    else if
    。难道
    $av
    不应该是一些不受约束的任意浮点数吗?听起来是个不错的解决方案,但如果平均值是4呢。7?这似乎是最简单的解决方案。是否有办法将小数向上或向下舍入。因此4.7将变为4.5,4.8将变为5?4.7是愚蠢的,一半(.5)或全部(1.0)是浮点数作为星的最佳视觉表示。@Sandwolf0x37:不是星是
    4.7
    ,而是投票的平均值(阅读@user537137的最后一条评论)。这就是为什么它必须四舍五入,以匹配星星。@user537137:我很确定这可以通过一点数学来实现。我宁愿使用
    if…else if…else
    条件,而不是令人困惑的
    开关盒
    。例如,这里的计算是
    $av==($av>1&$av<2)
    等等。这确实会产生误导。举个例子,如果$av是
    0
    ,那么上面的开关盒将回响1星。