Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create FontAwesome static rating bar based on rating data
<?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;
}
createFontAwesomeStarRating(7,0,10,5,true);
// OR
$output=createFontAwesomeStarRating(7,0,10,5,false);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment