12

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.

salim basar
  • 205
  • 1
  • 3
  • 9

5 Answers5

33
<?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

Wouter Rutgers
  • 541
  • 5
  • 14
8

When I have done this in the past, I used one image of 5 empty stars underneath one image of 5 filled stars. I then did something like

filled-stars.width = (empty-stars.width * (rating / 5)

This way you can display ratings of like 3.2978 etc.

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
2

You can do that with PHP, HTML and CSS:

<div class="star-<?=$number?>">
    <b></b><b></b><b></b><b></b><b></b>
</div>

You can then style that with CSS, so to display background images according to stars. If you convert the <b> tags to <a> tags it's probably more semantic.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • How would you style that with CSS? – Dyin Apr 20 '12 at 18:05
  • with the class. you can then count children and set their background image according to their parent's div classname. – hakre Apr 20 '12 at 19:11
  • I'm very interested in this. Can you provide an example code? Or maybe could you implement this topic with CSS? – Dyin Apr 20 '12 at 21:11
  • @Dyin: You could use nth-child or friends: http://www.w3.org/TR/selectors/#nth-child-pseudo – hakre Apr 20 '12 at 21:17
  • Doesn't this mean too much CSS code to implement? Could you solve this in your post? +1 for odd idea! – Dyin Apr 20 '12 at 21:24
  • You could be really sneaky and nest the tags, so that when you hover one of the stars it hovers all the previous stars too, so you could therefore use CSS to show the rating that the user wishes to submit. – Neil Apr 21 '12 at 23:23
  • @Neil: You normally don't need to nest the tags for that. – hakre Apr 22 '12 at 09:21
  • How else would the stars to the left know which one was being hovered? – Neil Apr 22 '12 at 20:56
1

Consider using sprites. Start with a graphic that contains a row of stars for each possible rating, and then compute the background offset by multiplying the height of each half-star graphic times the number of half-stars in the rating.

E.g.:

<?php
$offset =
    ($rating / .5)  // number of half-stars in $rating
  * 15;             // height of each sprite in stars.png
?>
<div style="background-image:url("stars.png");background-position:0 <?php echo $offset; ?>px;"></div>

Combined with a little bit of Javascript, you can implement a fully-featured ratings widget.

1

Here, this adds stars echo '*'; and halfs if needed echo '+';
Change '*' and '+' for example to <img src="star.gif" /> and <img src="halfstar.gif" />

// This number of stars:
$number = 2.7;

// Make it integer:
$stars = round( $number * 2, 0, PHP_ROUND_HALF_UP);

// Add full stars:
$i = 1;
while ($i <= $stars - 1) {
    echo '*';
    $i += 2;
}
// Add half star if needed:
if ( $stars & 1 ) echo '+';