Created
April 15, 2019 10:04
-
-
Save yhira/251a0498066f18dc84e9953a6e36d836 to your computer and use it in GitHub Desktop.
レーティングスタータグの取得
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
function get_rating_star_tag($rate, $max = 5, $number = false){ | |
$rate = floatval($rate); | |
$max = intval($max); | |
//数字じゃない場合 | |
if (!is_numeric($rate) || !is_numeric($max)) { | |
return $rate; | |
} | |
//レーティングが100より多い場合は多すぎるので処理しない | |
if ($rate > 100 && $max > 100) { | |
return $rate; | |
} | |
$tag = '<div class="ranking-item-rating rating-star">'; | |
//小数点で分割 | |
$rates = explode('.', $rate); | |
if (!isset($rates[0])) { | |
return $rate; | |
} | |
//小数点以下が5かどうか | |
if (isset($rates[1])) { | |
$has_herf = intval($rates[1]) == 5; | |
} else { | |
$has_herf = false; | |
} | |
if ($has_herf) { | |
$before = intval($rates[0]); | |
$middle = 1; | |
$after = $max - 1 - $before; | |
} else { | |
$before = intval($rate); | |
$middle = 0; | |
$after = $max - $before; | |
//3.2とかの場合は小数点以下を切り捨てる | |
$rate = floor(floatval($rate)); | |
} | |
//スターの出力 | |
for ($j=1; $j <= $before; $j++) { | |
$tag .= '<span class="fa fa-star"></span>'; | |
} | |
//半分スターの出力 | |
for ($j=1; $j <= $middle; $j++) { | |
$tag .= '<span class="fa fa-star-half-o"></span>'; | |
} | |
//空スターの出力 | |
for ($j=1; $j <= $after; $j++) { | |
$tag .= '<span class="fa fa-star-o"></span>'; | |
} | |
if ($number) { | |
$tag .= '<span class="rating-number">'.$rate.'</span>'; | |
} | |
$tag .= '</div>'; | |
return $tag; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment