Jump to content

Star rating with halves


-Karl-

Recommended Posts

What's the best way to display a star rating, which includes half values.

 

At the moment I have:

$value = 3.66;
$value = round(($value*2), 0)/2;

switch($value) {
    case 0:
        echo 0 stars;
    break;
    case 0.5:
        echo half a star;
    break;
}

 

What's a more efficient way to do this?

Link to comment
Share on other sites

Well, I could see where one would expect 3.9 to produce 4 full stars. Otherwise, it would be impossible for an item to have 5 full starts (assuming 5 is the max) if there is even one rating less than 5. I would round the value to the nearest 1/2 value.

 

Then assuming the "stars" are images you need to output the full, half and stars. You could use multiple images with all five stars together and simply name them using the numeric value: star_0, start_0.5, star_1.0, etc. Or you can do the logic in a loop, i.e. for $i=0 to 5 and do a comparison on each iteration. But, I think single images for all five stars is the easiest.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.