How to echo star from my rating table?

174 Views Asked by At

I am pretty new to star rating system, I have made a rating from 1 to 5 and it successfully store in my back end system, the rating store at back end was in a range of number 1 to 5. However, I would like to echo these numbers using image star, I put in the if statement and it does not show in my website.

     <?
     if ('rating' == '1'){
     echo '<img scr = "https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png"/>';
     }
     if ('rating' == '2'){
     echo '<img scr = "https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
                 https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png"/>';
     }
     if ('rating' == '3'){
     echo '<img scr = "https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
                 https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
                 https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png"/>';
     }
     if ('rating' == '4'){
     echo '<img scr = "https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
                https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
                 https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
                 https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png"/>';
     }
     if ('rating' == '5'){
     echo '<img scr = "https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
                 https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
                 https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
                 https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
                 https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png"/>';
     } 
   ?>

here is my code for the if statement, for you information, this is within the loop which I had added also $i=1 in the front and $i++ at the back, may I know did I put anything wrong?

1

There are 1 best solutions below

0
On

For anyone coming here for a solution, this should work.

$stars_displayed = 0; 
for ($i = 0; $i < $rating; $i++){
    $stars_displayed++;
    echo '<img src="fullstar_img.png" />';
}
for ($i = $stars_displayed; $i < 5; i++){
    echo '<img src="emptystar_img.png" />';
}