Created
March 28, 2015 00:18
-
-
Save fdcore/8047fe27f55b3758375f to your computer and use it in GitHub Desktop.
Функция генерации звёзд по рейтингу, 10 бальный рейтинг => в 5 звёзд
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function stars ($string, $color_class='') { | |
$html = '<ul class="rating '.$color_class.'">'; | |
$avg5 = round($string / 2, 1); | |
for($i=0; $i <= 5; $i++){ | |
$x = $i + 1; | |
if($avg5 == $i) $html.= _stars_gen($i); | |
if($avg5 < $x && $avg5 > $i) $html.= _stars_gen($i, true); | |
} | |
$html.='</ul>'; | |
return $html; | |
} | |
function _stars_gen($full_stars, $half=false){ | |
$FULL = '<li><a class="active" href="#"></a></li>'; | |
$HALF = '<li><a class="half" href="#"></a></li>'; | |
$NONE = '<li><a href="#"></a></li>'; | |
$empty = ceil(5 - $full_stars); | |
if($half){ | |
if($full_stars == 0){ | |
return $HALF.str_repeat($NONE, 5); | |
} else { | |
return str_repeat($FULL, $full_stars).$HALF.str_repeat($NONE, $empty); | |
} | |
} else { | |
if($full_stars == 0){ | |
return str_repeat($NONE, 5); | |
} else { | |
return str_repeat($FULL, $full_stars).str_repeat($NONE, $empty); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment