I'm developing the web app and made the (maybe foolish) decision to try and make a rating system rather than implement someone else's.
Below is a screenshot of the web app. The decimal values are the values retrieved from the database. I am using the following logic to turn the value into a star-representation.
for($x=1;$x<=$this->retreat->total_review_activities_rating;$x++) {
echo '<i class="fas fa-star"></i>';
}
if (strpos($this->retreat->total_review_activities_rating,'.')) {
echo '<i class="fas fa-star-half-alt"></i>';
$x++;
}
while ($x<=5) {
echo '<i class="far fa-star"></i>';
$x++;
}
As you can see from the screenshot, The value 5.000 is hitting the half star option in the loop. How can I adjust the second argument to avoid this from happening?
Any help appreciated.
$rating = $this->retreat->total_review_activities_rating;
The number of full stars is the whole number part of the rating.
$full_stars = (int) $rating;
echo str_repeat('<i class="fas fa-star"></i>', $full_stars);
You display a half star if the decimal portion of the rating exceeds some value of your choice.
$half_star = $rating - $full_stars > 0.2; // 0.2 for example
echo $half_star ? '<i class="fas fa-star-half-alt"></i>' : '';
The number of remaining empty stars is the total number of possible symbols (5) minus the number of symbols displayed so far. (The boolean $half_star
will be converted to int 0 or 1.)
$empty_stars = 5 - ($full_stars + $half_star);
echo str_repeat('<i class="far fa-star"></i>', $empty_stars);