I created this code to Show the Current Rating of a product using stars.
The number of stars to be displayed will be determined by the results we get when we divide the review points by 2.
For example:
Product Shoe has a review points of 4 that means 4/2 =2 (2 stars) or lets say 6 points 6/2 = 3 (3 stars). Since the maximum number of stars is 5. If the results we get when we divide points is greater than 5 the stars will stay at 5.
Here is my example code, but it's not working correctly and it's confusing.
$star = "<li><a href='#'><i class='fa fa-star' aria-hidden='true'></i></a></li>";
$total_stars = $product_review / 2;
for($i=1; $i<=5; $i++) {
if($product_review >= $i) {
if($total_stars) {
echo $star;
}
}
}
Thanks in advance.
If you want to still use your code above, Here is the fixed code.
<?php
$star = "<li><a href='#'><i class='fa fa-star' aria-hidden='true'></i></a></li>";
$total_stars = (int)($product_review / 2);
for ($i=0; $i < $total_stars ; $i++) {
if ($i === 5) {
break;
}else{
echo $star;
}
}
?>