function star_bar( $data = array( ) ) {
$output = "<b>Rating:</b> ";
if ( $data["rating"] == 1 ) {
$output .=
"<span class=\"star-icon half\">☆</span>
<span class=\"star-icon\">☆</span>
<span class=\"star-icon\">☆</span>
<span class=\"star-icon\">☆</span>
<span class=\"star-icon\">☆</span><br />";
}
if ( $data["rating"] == 2 ) {
$output .=
"<span class=\"star-icon full\">☆</span>
<span class=\"star-icon\">☆</span>
<span class=\"star-icon\">☆</span>
<span class=\"star-icon\">☆</span>
<span class=\"star-icon\">☆</span><br />";
}
if ( $data["rating"] == 3 ) {
$output .=
"<span class=\"star-icon full\">☆</span>
<span class=\"star-icon half\">☆</span>
<span class=\"star-icon\">☆</span>
<span class=\"star-icon\">☆</span>
<span class=\"star-icon\">☆</span><br />";
}
if ( $data["rating"] == 4 ) {
$output .=
"<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon\">☆</span>
<span class=\"star-icon\">☆</span>
<span class=\"star-icon\">☆</span><br />";
}
if ( $data["rating"] == 5 ) {
$output .=
"<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon half\">☆</span>
<span class=\"star-icon\">☆</span>
<span class=\"star-icon\">☆</span><br />";
}
if ( $data["rating"] == 6 ) {
$output .=
"<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon\">☆</span>
<span class=\"star-icon\">☆</span><br />";
}
if ( $data["rating"] == 7 ) {
$output .=
"<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon half\">☆</span>
<span class=\"star-icon\">☆</span><br />";
}
if ( $data["rating"] == 8 ) {
$output .=
"<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon\">☆</span><br />";
}
if ( $data["rating"] == 9 ) {
$output .=
"<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon half\">☆</span><br />";
}
if ( $data["rating"] == 10 ) {
$output .=
"<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span>
<span class=\"star-icon full\">☆</span><br />";
}
return $output;
}
正如你所看到的那样,这是非常不整洁的,是一种可能的循环吗?这将使用从mysql数据库中提取的整数输出星形条。
这是一个非常简单的css星条源[{3}}
答案 0 :(得分:3)
function star_bar($data = array())
{
$output = "<b>Rating:</b> ";
for ($i = 1; $i <= $data["rating"] / 2; $i++) {
$output .= "<span class=\"star-icon full\">☆</span>";
}
if ($data["rating"] % 2 == 1) {
$output .= "<span class=\"star-icon half\">☆</span>";
}
for ($i = 1; $i <= (10 - $data["rating"]) / 2; $i++) {
$output .= "<span class=\"star-icon\">☆</span>";
}
$output .= "<br />";
return $output;
}
每两个等级的满星数量增加:0和1你有零满星,有2和3你有一个,依此类推。因此,您将等级除以2,即可获得满星的数量。
你最多只有一个半星,而且只有奇怪的等级。因此,只有当您的评分除以2时,才会添加余数为1(评分不是偶数)。
空星的数量就像满星的数量一样,除了它基于(10 - $data["rating"])
(它随着等级增加而减少)这一事实。
答案 1 :(得分:1)
function getStars($rating)
{
$ratios = array();
$result = "";
$lastStarIndex =ceil($rating / 2) -1 ;
//fill all
for($i = 0; $i < 5; $i++)
{
if($i == $lastStarIndex)
$ratios[$i] = $rating % 2 ? "half" : "full";
else if($i < $lastStarIndex)
$ratios[$i] = "full";
else
$ratios[$i] = "";
}
for($i = 0; $i <5 ; $i++)
$result .= "<span class=\"star-icon " . $ratios[$i] . "\">*</span>";
return $result;
}
print_r(getStars(10));
print_r(getStars(1));
print_r(getStars(3));
print_r(getStars(6));