将星级降低至5

时间:2019-03-15 12:29:51

标签: php html

我正在尝试更改网站上的评分视图,如下图所示,它显示了7星​​

Click here to see the image

这是评分过滤器的代码

<div class="rating" style="font-size: 14px;">
<div class="go-right"><input id="1" type="radio" name="stars" class="go-right radio" value="1" <?php if(@$_GET['stars'] == "1"){echo "checked";}?>>&nbsp;&nbsp;<label class="go-left" for="1"><?php echo $star; ?><?php for($i=1;$i<=6;$i++){ ?> <?php echo $stars; ?> <?php } ?></label></div>
<div class="clearfix"></div>
<div class="go-right"><input id="2" type="radio" name="stars" class="radio go-right" value="2" <?php if(@$_GET['stars'] == "2"){echo "checked";}?>>&nbsp;&nbsp;<label class="go-left" for="2"><?php for($i=1;$i<=2;$i++){ ?> <?php echo $star; ?> <?php } ?><?php for($i=1;$i<=5;$i++){ ?> <?php echo $stars; ?> <?php } ?></label></div>
<div class="clearfix"></div>
<div class="go-right"><input id="3" type="radio" name="stars" class="radio" value="3" <?php if(@$_GET['stars'] == "3"){echo "checked";}?>>&nbsp;&nbsp;<label class="go-left" for="3"><?php for($i=1;$i<=3;$i++){ ?> <?php echo $star; ?> <?php } ?><?php for($i=1;$i<=4;$i++){ ?> <?php echo $stars; ?> <?php } ?></label></div>
<div class="clearfix"></div>
<div class="go-right"><input id="4" type="radio" name="stars" class="radio" value="4" <?php if(@$_GET['stars'] == "4"){echo "checked";}?>>&nbsp;&nbsp;<label class="go-left" for="4"><?php for($i=1;$i<=4;$i++){ ?> <?php echo $star; ?> <?php } ?><?php for($i=1;$i<=3;$i++){ ?> <?php echo $stars; ?> <?php } ?></label></div>
<div class="clearfix"></div>
<div class="go-right"><input id="5" type="radio" name="stars" class="radio" value="5" <?php if(@$_GET['stars'] == "5"){echo "checked";}?>>&nbsp;&nbsp;<label class="go-left" for="5"><?php for($i=1;$i<=5;$i++){ ?> <?php echo $star; ?> <?php } ?><?php for($i=1;$i<=2;$i++){ ?> <?php echo $stars; ?> <?php } ?></label></div>
<div class="clearfix"></div>

我只想显示5星而不是7星,我该怎么做?

2 个答案:

答案 0 :(得分:2)

将所有后续的for循环(打印出空白星号)更改为此:

for (;$i<=5;$i++)

即不要重置$i的值,那么任何一行最多可以打印5颗星。

例如第3行:

<div class="go-right"><input id="3" type="radio" name="stars" class="radio" value="3"
<?php if(@$_GET['stars'] == "3"){echo " checked";}?>>&nbsp;&nbsp;
<label class="go-left" for="3">
<?php for ($i=1; $i<=3; $i++) { echo $star; } 
      for (    ; $i<=5; $i++) { echo $stars; } ?>
</label>
</div>

通过循环,您可以将此代码用于所有行:

<?php for ($s = 1; $s <= 5; $s++) { ?>
  <div class="go-right"><input id="<?php echo $s; ?>" type="radio" name="stars" class="radio" value="<?php echo $s; ?>"
  <?php if(@$_GET['stars'] == $s){echo " checked";}?>>&nbsp;&nbsp;
  <label class="go-left" for="<?php echo $s; ?>">
  <?php for ($i=1; $i<=$s; $i++) { echo $star; } 
        for (    ; $i<=5; $i++) { echo $stars; } ?>
  </label>
  </div>
  <div class="clearfix"></div>
<?php } ?>

Demo on 3v4l.org

答案 1 :(得分:0)

只需删除不必要的代码。

<div class="rating" style="font-size: 14px;">
<div class="go-right"><input id="1" type="radio" name="stars" class="go-right radio" value="1" <?php if(@$_GET['stars'] == "1"){echo "checked";}?>>&nbsp;&nbsp;<label class="go-left" for="1"><?php echo $star; ?><?php for($i=1;$i<=6;$i++){ ?> <?php echo $stars; ?> <?php } ?></label></div>
<div class="clearfix"></div>
<div class="go-right"><input id="2" type="radio" name="stars" class="radio go-right" value="2" <?php if(@$_GET['stars'] == "2"){echo "checked";}?>>&nbsp;&nbsp;<label class="go-left" for="2"><?php for($i=1;$i<=2;$i++){ ?> <?php echo $star; ?> <?php } ?><?php for($i=1;$i<=5;$i++){ ?> <?php echo $stars; ?> <?php } ?></label></div>
<div class="clearfix"></div>
<div class="go-right"><input id="3" type="radio" name="stars" class="radio" value="3" <?php if(@$_GET['stars'] == "3"){echo "checked";}?>>&nbsp;&nbsp;<label class="go-left" for="3"><?php for($i=1;$i<=3;$i++){ ?> <?php echo $star; ?> <?php } ?><?php for($i=1;$i<=4;$i++){ ?> <?php echo $stars; ?> <?php } ?></label></div>
<div class="clearfix"></div>

星号是使用PHP实现的,因此您需要更改$stars php代码并将i减少到所需的数量。检查提供的答案@nick。