Skip to content

Instantly share code, notes, and snippets.

@ssovit
Last active December 19, 2018 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssovit/834090e4a618341a672584165fdf4fb5 to your computer and use it in GitHub Desktop.
Save ssovit/834090e4a618341a672584165fdf4fb5 to your computer and use it in GitHub Desktop.
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