Improve rating script (star rating)

Asked

Viewed 108 times

4

There’s a way to 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"> ';
            }
            ?>

That is his function: inserir a descrição da imagem aqui

The more he gets when he has less than 3 points. inserir a descrição da imagem aqui

  • 2

    What’s ifs for? What does it change if it’s 5 or if it’s two? I think that’s all it takes: for ($i=1;$i<=5;$i++) echo '<img src="'.$linkSite.'/img/star'.($i>$calculation?'1':'').'_16x16.png">';

  • 1

    Yes it is possible, believe me!

  • 1

    I suggest doing javascript and css because it is something visual that does not need to be processed on the server. Unless you have a very specific reason for rendering on the server. Take an example: http://stackoverflow.com/questions/1987524/turn-a-number-into-star-rating-display-using-jquery-and-css/1987545#1987545

  • Actually, this is a case for pure CSS, and only change the class by PHP.

  • 1

    @Bacco Thanks!!

  • @Danielomine I’ll try this there, it looks like it will make the system better :D

Show 1 more comment

1 answer

6


I think something like that 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" />';
    }
}
  • It became a more complex version of what I put in the comment :) However, I understand that it is right. + 1

  • True, I saw yours only after rsrs, put it as an answer

  • 1

    Answer of a line is only dirty :) If you want to "import" it in yours, feel free. Just check the syntax, I wrote a little fast just to illustrate.

  • Rsrs, but it is the answer that he needs kkkkk, is that I like to do in several lines, I think better to read the code

  • @Jefersonassis Yes, it worked! Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.