skip to Main Content

I have a script which reads an array from a text file and grabs each line and produces star rating images based on number in array. The script is kind of big and was wondering if there was a better way of doing this.

<?php if ($row[2] == '5') { ?>
    <i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>
<?php } elseif ($row[2] == '4.5') { ?>
    <i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i>
<?php } elseif ($row[2] == '4') { ?>
    <i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '3.5') { ?>
    <i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '3') { ?>
    <i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '2.5') { ?>
    <i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '2') { ?>
    <i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '1.5') { ?>
    <i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '1') { ?>
    <i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '.5') { ?>
    <i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '0') { ?>
    <i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } ?>

3

Answers


  1. You can work off this function (doesn’t include the half stars, shows you how to do the full star rating – I just noticed you run halfs as well);

    function getStars($rating){
        $stars = "";
        $x = 0;
        while($x < $rating) {
          $stars = $stars.'<i class="fa fa-star"></i>';
          $x++;
        }
        return $stars;
    }
    
    echo getStars(3);
    

    in your case –

    echo getStars($row[2]);
    

    You can tweak this that if the number contains a decimal you can append to the string ” for example; you get the gist 😛

    ————– Edit

    Tweaked it and added the half star rating 🙂

    function getStars($rating){
        $stars = "";
        $x = 1;
        while($x <= $rating) {
          $stars = $stars.'<i class="fa fa-star"></i>';
          $x++;
        }
        if (strpos($rating, '.') !== false) {
            $stars = $stars . '<i class="fa fa-star-half">';
        }
        return $stars;
    }
    
    echo getStars(3.5);
    
    Login or Signup to reply.
  2. function getStar($rating) {
      $wholeStar = "<i class="fa fa-star"></i>";
      $halfStar = "<i class="fa fa-star-half-o"></i>";
    
      // round the rating for whole stars
      $roundedRating = round($rating);
    
      // Add the whole stars using str_repeat
      $starString = str_repeat($wholeStar, round($rating));
    
      // Add the half star if the rating is bigger than it's rounded value
      if ($roundedRating < $rating) {
        $starString .= $halfStar;
      }
    
      return $starString
    }
    
    echo getStar($row[2]);
    
    
    Login or Signup to reply.
  3. Yes, better ways… A loop will help, maybe something like this (PHP demo at tio.run).

    function star_printer($num)
    {
      $star_class = [
      'blank' => 'far fa-star',
      'half' => 'fas fa-star-half',
      'full' => 'fas fa-star'];
      
      $star_html = '<i class="%s"></i>';
      
      $star_str = "";
      for($i=1; $i<=5; $i++)
      {
        $is_half = $i==ceil($num) && ceil($num)!=$num;
        $star = $is_half ? "half" : ($i>$num ? "blank" : "full");
        $star_str .= sprintf($star_html, $star_class[$star]);
      }
    
      return $star_str;
    }
    
    echo star_printer(3.5);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search