简体   繁体   中英

how to convert number into star rating using php?

I have a page where i need to show star rating system. converting number to visually star image. i have 1.5,2,2.5,..5.0. I have a code shows converting number but 5.0 showing 6 star

  $starNumber = 5.0;

  for ($x = 1; $x <= $starNumber; $x++) {
      echo '<li><i class="fa fa-star"></i></li>';
  }
  if (strpos($starNumber, '.')) {
      echo '<li><i class="fa fa-star-half-o"></i></li>';
      $x++;
  }
  while ($x <= 5) {
      echo '<li><i class="fa fa-star-o"></i></li>';
      $x++;
  }

My solution:

for( $x = 0; $x < 5; $x++ )
{
    if( floor( $starNumber )-$x >= 1 )
    { echo '<li><i class="fa fa-star"></i></li>'; }
    elseif( $starNumber-$x > 0 )
    { echo '<li><i class="fa fa-star-half-o"></i></li>'; }
    else
    { echo '<li><i class="fa fa-star-o"></i></li>'; }
}

PhpFiddle demo

With only one foor loop I compare floor value (float rounded down) of $starNumber to curren $x value to echo full-star; otherwise if not rounded value is greater than current $x I echo half-star; otherwise (value lower than current $x ) I echo empty-star.

Here I would like to help other persons who are actually looking for twig template solution....

{% for x in 0..4 %}
   {% if book_data.averageRating|round(2, 'floor') - x >= 1 %}
        <i class="fa fa-star gold"></i>
        {% elseif book_data.averageRating - x > 0 %}
        <i class="fa fa-star-half-o gold"></i>
          {% else %}
        <i class="fa fa-star-o"></i>
   {% endif %}
{% endfor %}
$starLimit  = 5;
$liStarRate = "";
for($star=1; $star<=$starLimit; $star++) {
    //this holds .5 values
    $liStarRate.='<li star-rate="'.($star-.5).'"><i class="fa fa-star-half-o"></i></li>';
    //this holds the whole values
    $liStarRate.='<li star-rate="$star"><i class="fa fa-star-o"></i></li>';
}

use a simple looping statement as above, where you don't require any conditional statements, as every whole number can be changed to float by simply subtracting 0.5 on it.

PHP:

function stars($rating){
    for($i = 0; $i < 5; $i++){
        if($i < $rating){
            echo "<i class='fas fa-star glow'></i>";
        } else {
            echo "<i class='fas fa-star'></i>";
        }
    }
}

CSS:

.glow {
   color: yellow;
}

Then use it like so:

stars(4);

I used FontAwesome v5.5.0 for this.

I actually just ended up working on something like this. Was a little disappointed to find that the answers here seemed to work only for full stars (rating 1-5), while I wanted something with half stars (rating 1-10)

I ended up creating this:

function starRating($rating){
    //Rating should be from 1-10. This function will produce a 5 star rating with half-stars

    $html = '';

    $full_stars = floor($rating/2);
    //10 = 5, 8 = 4, etc
    $half_star = $rating%2;
    //Should be 1 for every odd number.     
    $empty_stars = 5 - $full_stars - $half_star;

    for($i = 0; $i < $full_stars; $i++){
        //Add all of the full stars
        $html .= '<span class="fa fa-star checked"></span>';
    }

    if($half_star){
        //We can get away with this because 0 == false
        //Add half star         
        $html .= '<span class="fa fa-star-half-alt checked"></span>';
    }

    for($i = 0; $i < $empty_stars; $i++){
        //Fill any remainder with empty stars.
        $html .= '<span class="fa fa-star"></span>';
    }
    return $html;
}

The output for 0 will be 5 blank stars. The output for 10 will be 5 full stars. Output for 5 will be 2 full stars, 1 half star, 2 blank stars.

$starsOutput = '';
$wholeStars = floor($rating);
$halfStars = ($rating - $wholeStars) * 2;
$emptyStars = 5 - ($wholeStars + $halfStars);
for ($i=0; $i < $wholeStars; $i++) { 
    $starsOutput .= '<i class="fas fa-star"></i>';
}
for ($i=0; $i < $halfStars; $i++) { 
    $starsOutput .= '<i class="fas fa-star-half-alt"></i>';
}
for ($i=0; $i < $emptyStars; $i++) { 
    $starsOutput .= '<i class="far fa-star"></i>';
}
<?php 

              $id = $_GET['id'];
            $edit = mysqli_query($conn, "SELECT * FROM feedback WHERE id=$id");
            $edit1 = mysqli_fetch_array($edit);
              ?>
<div class="form-group">
                  <label>Rating</label><br>
                  <?php $abc=$edit1['4']; ?> <--value columnnumber of table-->
<fieldset class="rating">
    <input type="radio" id="star5" <?php if($abc==5){echo 'checked';}?>  name="rating" value="<?php echo $abc; ?>" /><label class = "full" for="star5" title="Awesome - 5 stars"></label>
    <input type="radio" id="star4half" <?php if($abc==4.5){echo 'checked';}?> name="rating" value="<?php echo $abc; ?>" /><label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
    <input type="radio" id="star4" <?php if($abc==4){echo 'checked';}?> name="rating" value="<?php echo $abc; ?>" /><label class = "full" for="star4" title="Pretty good - 4 stars"></label>
    <input type="radio" id="star3half" <?php if($abc==3.5){echo 'checked';}?> name="rating" value="3.5" /><label class="half" for="star3half" title="Meh - 3.5 stars"></label>
    <input type="radio" id="star3" name="rating" <?php if($abc==3){echo 'checked';}?> value="3" /><label class = "full" for="star3" title="Meh - 3 stars"></label>
    <input type="radio" id="star2half" name="rating" <?php if($abc==2.5){echo 'checked';}?> value="2.5" /><label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
    <input type="radio" id="star2" name="rating" <?php if($abc==2){echo 'checked';}?> value="2" /><label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
    <input type="radio" id="star1half" name="rating" <?php if($abc==1.5){echo 'checked';}?> value="1.5" /><label class="half" for="star1half" title="Meh - 1.5 stars"></label>
    <input type="radio" id="star1" name="rating" value="1" <?php if($abc==1){echo 'checked';}?> /><label class = "full" for="star1" title="Sucks big time - 1 star"></label>
    <input type="radio" id="starhalf" name="rating" <?php if($abc==.5){echo 'checked';}?> value=".5" /><label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>enter code here


</fieldset>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM