Converting numbers to visual rating (stars)?

salim basar picture salim basar · Apr 20, 2012 · Viewed 24.9k times · Source

in my database ı have numbers 1, 2, 2.5, 3, 3.5, 4, 4.5 and 5

I want to convert these numbers to stars.

I have a full star and a half star.

How ı can do that after getting information from database?

I have the rating tag on the database.

Answer

Wouter Rutgers picture Wouter Rutgers · Apr 20, 2012
<?php
    for($x=1;$x<=$starNumber;$x++) {
        echo '<img src="path/to/star.png" />';
    }
    if (strpos($starNumber,'.')) {
        echo '<img src="path/to/half/star.png" />';
        $x++;
    }
    while ($x<=5) {
        echo '<img src="path/to/blank/star.png" />';
        $x++;
    }
?>

*Assuming you are using PHP