Share on facebook
Share on twitter
Share on linkedin
Share on email
<?php function createFontAwesomeStarRating($rating=0,$min=0,$max=10,$maxStars=5,$return=false){
    $score=(($rating - $min) * ($maxStars)) / ($max - $min) ;
    $full=floor($score);
    $half=ceil($score)-$full;
    $empty=$maxStars-$full-$half;
    $result='<div class="rating-stars" title="'.$score.'">';
    $result.=str_repeat('<i class="fa fa-star"></i>',$full);
    $result.=str_repeat('<i class="fa fa-star-half-o"></i>',$half);
    $result.=str_repeat('<i class="fa fa-star-o"></i>',$empty);
    $result.="</div>";
    if($return){
        return $result;
    }
    echo $result;
}
?>

The function above allows to ouput the star rating html based on score provided.

<?php 
$ratingHtml=createFontAwesomeStarRating(7,0,10,5,true); // will return rating html as string 
?>
<?php 
createFontAwesomeStarRating(7,0,10,5); // Will echo the rating html
?>
Share on facebook
Share on twitter
Share on linkedin
Share on email