2017-08-10 86 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