![Retrieve numbers and turn as star [duplicate]](https://www.xszz.org/skin/wt/rpic/t9.jpg)
Question:
This question already has an answer here:
<ul><li> <a href="/questions/44491290/display-star-in-front-end-based-on-rating" dir="ltr" rel="nofollow">Display Star in Front End based on rating</a> <span class="question-originals-answer-count"> 3 answers </span> </li> </ul>Is that possible to I show my database number eg. 3
as font-awesome numbers using PHP?
I have Testimonials table where people write their feedback and give stars (these stars save as number 1 to 5), now i can show then in my template as number it is, but i want to know is there anyway to show them as font-icon?
Using: Laravel PHP framework.
UPDATEI added this to my testimonial
model and now i return stars but issue is all of them are empty stars and not get color to selected numbers.
public function tesstars()
{
$rates = '';
$stars = floor($rates);
$rates = '';
$j = 0;
for ($i = 0; $i < 5; $i++) {
for ($j; $j < $stars; $j++) {
$rates .= '<span class="fa fa-star"></span>';
$i++;
}
if ($i < 5) {
$rates .= '<span class="fa fa-star-o"></span>';
}
}
return $rates;
}
<a href="https://i.stack.imgur.com/GqlnT.png" rel="nofollow"><img alt="image one" class="b-lazy" data-src="https://i.stack.imgur.com/GqlnT.png" data-original="https://i.stack.imgur.com/GqlnT.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>
Answer1:You can do it but just simple foor loop like this
@for ($i = 0; $i < $testimonial->star; $i++)
<span class="fa fa-star"></span>
@endfor
@for ($i = $testimonial->star; $i < 5; $i++)
<span class="fa fa-star-o"></span>
@endfor
This is simple Javascript Example with for loop
<pre class="snippet-code-js lang-js prettyprint-override">var star = 3;
makeStar(star);
function makeStar(star){
var str = "";
for(var j=1;j<=star;j++){
str += '<span onclick="makeStar('+j+')" class="fa fa-star"></span>';
}
for(var k=star+1;k<=5;k++){
str += '<span onclick="makeStar('+k+')" class="fa fa-star-o"></span>';
}
document.querySelector("#output").innerHTML = str;
}
<pre class="snippet-code-css lang-css prettyprint-override">.fa{
width:50px;
height:50px;
font-size:50px;
}
.fa-star::before{
content:'*';
color:green;
}
.fa-star-o::before{
content:'*';
color:lightgrey;
}
<pre class="snippet-code-html lang-html prettyprint-override"><div id="output"></div>
Answer2:You can do this in your view:
@if ($rating > 0)
@while ($rating > 0)
<i class="fa fa-star" aria-hidden="true"></i>
{{ $rating = $rating - 1 }}
@endwhile
@endif
Answer3:Try this:
$stars = 3;
for($i = 0; $i < 5; $i++)
{
if($i < $stars){
$rates .= '<span class="fa fa-star"></span>';//coloured star
}else{
$rates .= '<span class="fa fa-star-o"></span>';//colourless star
}
}
Answer4:SOLVED
Well as I couldn't make it happen from model I decided to make else,elseif in my blade and load stars manually in there.
Thanks for all helps.
@if($testimonial->star == 1)
<span class="fa fa-star"></span>
<span class="fa fa-star-o"></span>
<span class="fa fa-star-o"></span>
<span class="fa fa-star-o"></span>
<span class="fa fa-star-o"></span>
@elseif($testimonial->star == 2)
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star-o"></span>
<span class="fa fa-star-o"></span>
<span class="fa fa-star-o"></span>
@elseif($testimonial->star == 3)
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star-o"></span>
<span class="fa fa-star-o"></span>
@elseif($testimonial->star == 4)
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star-o"></span>
@else($testimonial->star == 5)
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
@endif