2017-08-10 66 views
0

我已经从数组PHP数值为HTML阵列

<p><b>Rating</b>: <?= $arr->rating; ?></p> 

,其输出在浏览器源代码的数值

<p><b>Rating</b>: 5.100000</p> 

(这是一个等级的5.100000总分10分)

我将如何去显示评价数值作为一个五星级的数组?

例如,5.100000的值将显示为五个像这样的两个和1/2个星。

<li> 
    <i class="glyphicon glyphicon-star"></i> 
    <i class="glyphicon glyphicon-star"></i> 
    <i class="glyphicon glyphicon-star half"></i> 
    <i class="glyphicon glyphicon-star empty"></i> 
    <i class="glyphicon glyphicon-star empty"></i> 
</li> 
+4

html ... array ...?这不是一回事。你的问题很难理解。 – GrumpyCrouton

+0

你能否给我们一个你最终结果需要的例子? –

+2

我想你想说的是,你想要根据数字自动生成所需数量的恒星(和半星)。 – Difster

回答

2

for循环使用和本次循环比较$arr->rating以确定是否明星应该是全,半或空。

// first convert the rating from score out of ten to score out of five 
$rating = $arr->rating/2; 

for ($i=0; $i < 5; $i++) { 
    $current = $rating - $i; 
    if ($current >= 1) { 
     $class = ''; 
    } elseif ($current > 0) { 
     $class = 'half'; 
    } else { 
     $class = 'empty'; 
    } 
    echo "<i class=\"glyphicon glyphicon-star $class\"></i>"; 
} ?> 
+1

我认为$ current = $ arr-> rating - $ i;应该是$ current = $ rating - $ i; –

+0

哎呀,你说得对。 –

+0

感谢你们做到了! – dreadycarpenter