我正在尝试更改用于显示评论图像的代码。先前的代码包含5张星星图片。第一张图片有一颗星星,下一张有两颗,依此类推。每个图像的宽度相同,因此对齐始终正确。使用的代码是
$p_stars = '<img src="images/stars_' . $rating . '.png">';
根据结果,上面的结果会产生一连串的星星,例如
****
*****
*
***
我的想法是将图像替换为超棒的字体图标,以使其更易于控制颜色,如果需要更改颜色或星数,则需要较少的维护。我做到了,它可以正常工作,但是所需要的代码量远远超过了图像。所以我的问题是:
我应该坚持使用图像吗?
有没有更好的方法来为icon方法编码?
这是icon方法的代码:
<style>
.stars {color:#FB9802;}
.stars-hide {color:#fff;}
</style>
$p_stars = '';
$p = 0;
while ($p < 5) {
for ($x = 0; $x < $rating; ++$x) {
$p_stars .= '<i class="fas fa-star stars"></i>';
$p++;
}
if ($p == 5) break;
for ($x = $p; $x < 5; ++$x) {
$p_stars .= '<i class="fas fa-star stars-hide"></i>';
$p++;
}
}