user537137 user537137 - 25 days ago 10
PHP Question

Best star rating solution for PHP

I'm wondering what the best and most efficient way to write up the following PHP code would be?

if ($av == 1) echo '/images/1-star.png';
if ($av > 1 && < 2) echo '/images/1-half-star.png';
if ($av == 2) echo '/images/2-star.png';
if ($av > 2 && < 3) echo '/images/2-half-star.png';


Following this same pattern up to 5 stars.

Answer

Just use like this:

$n = is_int($av) ? $av : floor($av) + 0.5;
echo '/images/'.$n.'-star.png';

enter image description here

Cut images per line and name it "1-star.png", "1.5-star.png", "2-star.png", "2.5-star.png", "3-start.png", "3.5-star.png" and so on...