javascript - how to convert number into star rating using php? -


i have page need show star rating system. converting number visually star image. have 1.5,2,2.5,..5.0. have code shows converting number 5.0 showing 6 star

followed code

converting numbers visual rating (stars)?

  $starnumber = 5.0;    ($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 6 stars

my 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>'; } } 

phpfiddle demo

with 1 foor loop compare floor value (float rounded down) of $starnumber curren $x value echo full-star; otherwise if not rounded value greater current $x echo half-star; otherwise (value lower current $x) echo empty-star.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -