Improve rating script (star rating)

4

Can you improve this code?

<?php
            if ($calculation >= 5)
            {
                for ($i=1;$i<=5;$i++)
                {
                    echo '<img src="'.$linkSite.'/img/star1_16x16.png"> ';
                }
            }
            elseif ($calculation >=3 && $calculation <5)
            {
                for ($i=1;$i<=$calculation;$i++)
                {
                    echo '<img src="'.$linkSite.'/img/star1_16x16.png"> ';
                }
                for ($calculation;$calculation<5;$calculation++)
                {
                    echo '<img src="'.$linkSite.'/img/star_16x16.png"> ';
                }
            }
            elseif ($calculation <=2)
            {
                for ($i=1;$i<=$calculation;$i++)
                {
                    echo '<img src="'.$linkSite.'/img/star1_16x16.png"> ';
                }
                for ($calculation;$calculation<5;$calculation++)
                {
                    echo '<img src="'.$linkSite.'/img/star_16x16.png"> ';
                }
            }
            else
            {
                echo '<img src="'.$linkSite.'/img/star_16x16.png"> ';
            }
            ?>

This is his role:

Plushebugawhenhehaslessthan3points.

    
asked by anonymous 20.01.2016 / 20:08

1 answer

6

I think something like this would be enough

$nota = 3.6;

for($i=1; $i<=5; $i++){
    if($i <= $nota) {
        echo '<img src="estrela_ativa.png" />';
    }
    else {
        echo '<img src="estrela_inativa.png" />';
    }
}
    
20.01.2016 / 20:15