我想制作星级评分显示。这是我的代码。
<?php $rate = 4; ?>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star_border</i>
我想成功:例如,如果$ rate = 3显示3x star,2x star_border。 if $ rate = 4显示4x star,1x star_border
这是我太长的代码。我想要更短,更实用。
<?php $rate = 1;?>
<?php if ($rate == 1): ?>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star_border</i>
<i class="material-icons md-18">star_border</i>
<i class="material-icons md-18">star_border</i>
<i class="material-icons md-18">star_border</i>
<?php elseif ($rate == 2): ?>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star_border</i>
<i class="material-icons md-18">star_border</i>
<i class="material-icons md-18">star_border</i>
<?php elseif ($rate == 3): ?>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star_border</i>
<i class="material-icons md-18">star_border</i>
<?php elseif ($rate == 4): ?>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star_border</i>
<?php elseif ($rate == 5): ?>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star</i>
<i class="material-icons md-18">star</i>
<?php endif;?>
答案 0 :(得分:2)
这应该对你有用。
<?php
$rate = 3;
for ($x = 0; $x < $rate; $x++) {
echo '<i class="material-icons md-18">star</i>';
}
for ($x = 0; $x < 5-$rate; $x++) {
echo '<i class="material-icons md-18">star_border</i>';
}
?>
你回复尽可能多的评分所说的开头。然后你根据需要回复星形边界。