Comments on my website can receive a rating from other members from 1 to 5, and I calculate the “Avg Rating” for each.
Now I need to display the appropriate number of Star icons next to each Comment.
The tricky part is that an Avg Rating could be something like 4.3.
Not sure the best way to handle this, but I am leaning towards just displaying a “1/2 Star” to represent any fractional amount. (No one is going to be able to distinguish 1/4 of a star vs. 1/3 of a star and so on, right?!)
I have a simple FOR loop that will display the “whole Stars”, but I am trying to figure out how to tell PHP this…
When you look at the Avg Rating, take the part that is to the left of the decimal and display that many “whole Stars”, and then if there is any part to the right of the decimal, well just display a single “1/2 Star” icon for that.
Follow me?
Actually, Paul O’Brien - here on SitePoint - taught me how to create fractional Star icons using CSS, but I personally think it is overkill to be showing 1/10 of a Star and so on?! So I thought maybe just showing a 1/2 Star for any fractional amount was good enough.
But whether I show 4 whole Stars and a 1/2 Star to represent an Avg Rating of 4.3, or I go crazy and display 4 whole Stars and a 3/10 Star, what is clearly holding me back is not creating the Star icons themselves, but figuring out the PHP part.
I Googled how to break a Decimal up into the Whole part and Fractional part, but all of the solutions seem to fail with decimals less than 1 (e.g. 0.25) and negative decimals (e.g. -1.40).
Now, granted, I won’t have a negative Rating, and since you can only choose 1 to 5, I guess a Comment will never have n Avg Rating less than 1.0 Nonetheless, it would be nice to find a way to separate a decimal that works for all types of decimals in case I need to use that some place else.
Hope all of this makes some sense?! (:
Sincerely,
Debbie