admin管理员组文章数量:1293417
I have a page where i need to show star rating system. converting number to visually star image. i have 1.5,2,2.5,..5.0. I have a code shows converting number but 5.0 showing 6 star
followed this code
Converting numbers to visual rating (stars)?
$starNumber = 5.0;
for ($x = 1; $x <= $starNumber; $x++) {
echo '<li><i class="fa fa-star"></i></li>';
}
if (strpos($starNumber, '.')) {
echo '<li><i class="fa fa-star-half-o"></i></li>';
$x++;
}
while ($x <= 5) {
echo '<li><i class="fa fa-star-o"></i></li>';
$x++;
}
Showing as 6 stars
I have a page where i need to show star rating system. converting number to visually star image. i have 1.5,2,2.5,..5.0. I have a code shows converting number but 5.0 showing 6 star
followed this code
Converting numbers to visual rating (stars)?
$starNumber = 5.0;
for ($x = 1; $x <= $starNumber; $x++) {
echo '<li><i class="fa fa-star"></i></li>';
}
if (strpos($starNumber, '.')) {
echo '<li><i class="fa fa-star-half-o"></i></li>';
$x++;
}
while ($x <= 5) {
echo '<li><i class="fa fa-star-o"></i></li>';
$x++;
}
Showing as 6 stars
Share edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Feb 8, 2016 at 15:01 ParkerParker 1411 gold badge3 silver badges21 bronze badges 7- if (strpos($starNumber,'.') && $starNumber!=5.0 ) { – Parker Commented Feb 8, 2016 at 15:06
- Are you sure, because i have try the code above and it's work. It show 5 stars like you want. I think the problem is not in the code that you post but in the rest of your code. – Berserk Commented Feb 8, 2016 at 15:19
- did you check 5.0 rating i am getting 6 stars – Parker Commented Feb 8, 2016 at 15:22
- yes, the same code that yours – Berserk Commented Feb 8, 2016 at 15:25
-
you have to replace
while($x<=5)
withwhile($x<5)
... – fusion3k Commented Feb 8, 2016 at 15:26
7 Answers
Reset to default 7My solution:
for( $x = 0; $x < 5; $x++ )
{
if( floor( $starNumber )-$x >= 1 )
{ echo '<li><i class="fa fa-star"></i></li>'; }
elseif( $starNumber-$x > 0 )
{ echo '<li><i class="fa fa-star-half-o"></i></li>'; }
else
{ echo '<li><i class="fa fa-star-o"></i></li>'; }
}
3v4l demo
With only one foor
loop I pare floor
value (float rounded down) of $starNumber
to curren $x
value to echo full-star; otherwise if not rounded value is greater than current $x
I echo half-star; otherwise (value lower than current $x
) I echo empty-star.
Here I would like to help other persons who are actually looking for twig template solution....
{% for x in 0..4 %}
{% if book_data.averageRating|round(2, 'floor') - x >= 1 %}
<i class="fa fa-star gold"></i>
{% elseif book_data.averageRating - x > 0 %}
<i class="fa fa-star-half-o gold"></i>
{% else %}
<i class="fa fa-star-o"></i>
{% endif %}
{% endfor %}
I actually just ended up working on something like this. Was a little disappointed to find that the answers here seemed to work only for full stars (rating 1-5), while I wanted something with half stars (rating 1-10)
I ended up creating this:
function starRating($rating){
//Rating should be from 1-10. This function will produce a 5 star rating with half-stars
$html = '';
$full_stars = floor($rating/2);
//10 = 5, 8 = 4, etc
$half_star = $rating%2;
//Should be 1 for every odd number.
$empty_stars = 5 - $full_stars - $half_star;
for($i = 0; $i < $full_stars; $i++){
//Add all of the full stars
$html .= '<span class="fa fa-star checked"></span>';
}
if($half_star){
//We can get away with this because 0 == false
//Add half star
$html .= '<span class="fa fa-star-half-alt checked"></span>';
}
for($i = 0; $i < $empty_stars; $i++){
//Fill any remainder with empty stars.
$html .= '<span class="fa fa-star"></span>';
}
return $html;
}
The output for 0 will be 5 blank stars. The output for 10 will be 5 full stars. Output for 5 will be 2 full stars, 1 half star, 2 blank stars.
$starLimit = 5;
$liStarRate = "";
for($star=1; $star<=$starLimit; $star++) {
//this holds .5 values
$liStarRate.='<li star-rate="'.($star-.5).'"><i class="fa fa-star-half-o"></i></li>';
//this holds the whole values
$liStarRate.='<li star-rate="$star"><i class="fa fa-star-o"></i></li>';
}
use a simple looping statement as above, where you don't require any conditional statements, as every whole number can be changed to float by simply subtracting 0.5 on it.
PHP:
function stars($rating){
for($i = 0; $i < 5; $i++){
if($i < $rating){
echo "<i class='fas fa-star glow'></i>";
} else {
echo "<i class='fas fa-star'></i>";
}
}
}
CSS:
.glow {
color: yellow;
}
Then use it like so:
stars(4);
I used FontAwesome v5.5.0 for this.
$starsOutput = '';
$wholeStars = floor($rating);
$halfStars = ($rating - $wholeStars) * 2;
$emptyStars = 5 - ($wholeStars + $halfStars);
for ($i=0; $i < $wholeStars; $i++) {
$starsOutput .= '<i class="fas fa-star"></i>';
}
for ($i=0; $i < $halfStars; $i++) {
$starsOutput .= '<i class="fas fa-star-half-alt"></i>';
}
for ($i=0; $i < $emptyStars; $i++) {
$starsOutput .= '<i class="far fa-star"></i>';
}
<?php
$id = $_GET['id'];
$edit = mysqli_query($conn, "SELECT * FROM feedback WHERE id=$id");
$edit1 = mysqli_fetch_array($edit);
?>
<div class="form-group">
<label>Rating</label><br>
<?php $abc=$edit1['4']; ?> <--value columnnumber of table-->
<fieldset class="rating">
<input type="radio" id="star5" <?php if($abc==5){echo 'checked';}?> name="rating" value="<?php echo $abc; ?>" /><label class = "full" for="star5" title="Awesome - 5 stars"></label>
<input type="radio" id="star4half" <?php if($abc==4.5){echo 'checked';}?> name="rating" value="<?php echo $abc; ?>" /><label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
<input type="radio" id="star4" <?php if($abc==4){echo 'checked';}?> name="rating" value="<?php echo $abc; ?>" /><label class = "full" for="star4" title="Pretty good - 4 stars"></label>
<input type="radio" id="star3half" <?php if($abc==3.5){echo 'checked';}?> name="rating" value="3.5" /><label class="half" for="star3half" title="Meh - 3.5 stars"></label>
<input type="radio" id="star3" name="rating" <?php if($abc==3){echo 'checked';}?> value="3" /><label class = "full" for="star3" title="Meh - 3 stars"></label>
<input type="radio" id="star2half" name="rating" <?php if($abc==2.5){echo 'checked';}?> value="2.5" /><label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
<input type="radio" id="star2" name="rating" <?php if($abc==2){echo 'checked';}?> value="2" /><label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
<input type="radio" id="star1half" name="rating" <?php if($abc==1.5){echo 'checked';}?> value="1.5" /><label class="half" for="star1half" title="Meh - 1.5 stars"></label>
<input type="radio" id="star1" name="rating" value="1" <?php if($abc==1){echo 'checked';}?> /><label class = "full" for="star1" title="Sucks big time - 1 star"></label>
<input type="radio" id="starhalf" name="rating" <?php if($abc==.5){echo 'checked';}?> value=".5" /><label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>enter code here
</fieldset>
本文标签: javascripthow to convert number into star rating using phpStack Overflow
版权声明:本文标题:javascript - how to convert number into star rating using php? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741575087a2386235.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论