1

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 } ?>
aBe
  • 55
  • 4

3 Answers3

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

-------------- 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);
Ilan P
  • 1,602
  • 1
  • 8
  • 13
  • [The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place.](https://stackoverflow.com/help/duplicates#:~:text=The%20fundamental%20goal%20of%20closing%20duplicate%20questions%20is%20to%20help%20people%20find%20the%20right%20answer%20by%20getting%20all%20of%20those%20answers%20in%20one%20place.) – mickmackusa Dec 29 '22 at 20:33
1
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]);

thelastshadow
  • 3,406
  • 3
  • 33
  • 36
  • [The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place.](https://stackoverflow.com/help/duplicates#:~:text=The%20fundamental%20goal%20of%20closing%20duplicate%20questions%20is%20to%20help%20people%20find%20the%20right%20answer%20by%20getting%20all%20of%20those%20answers%20in%20one%20place.) – mickmackusa Dec 29 '22 at 20:34
1

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);
bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • [The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place.](https://stackoverflow.com/help/duplicates#:~:text=The%20fundamental%20goal%20of%20closing%20duplicate%20questions%20is%20to%20help%20people%20find%20the%20right%20answer%20by%20getting%20all%20of%20those%20answers%20in%20one%20place.) – mickmackusa Dec 29 '22 at 20:33
  • @mickmackusa That's fine, nice answers. Would not have thought of `str_repeat`. – bobble bubble Dec 29 '22 at 21:23