J.Fredy
J.Fredy

Reputation: 49

Reduce stars for rating to 5

I am trying to change the view of ratings on my website, it shows 7 stars as you can see in the image below

Click here to see the image

and this is the code of the rating filter

<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>

I want to show only 5 stars and not 7 stars, how I can do that?

Upvotes: 0

Views: 50

Answers (2)

pr0cz
pr0cz

Reputation: 507

Just delete the unnecessary code.

<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>

The stars were implemented with PHP, so you need to change the $stars php code and reduce your i to the wanted amount. Check the answer @nick provided.

Upvotes: 0

Nick
Nick

Reputation: 147146

Change all your subsequent for loops (that print out the blank stars) to this:

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

i.e. don't reset the value of $i, then you will print out at most 5 stars for any row.

So for example for row 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>

You can use this code for all the rows, by making a loop:

<?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

Upvotes: 2

Related Questions