How to fill a star rating?

Asked

Viewed 1,247 times

6

How to fill the stars of a "star rating".

The calculation is apparently working. Giving an "echo" in the variable $calc the result is displayed. Example, if the note is 3.3, it must fill 3 stars and 30% of the 4 star rating.

The question is: where should I place this variable to be displayed as stars?

HTML code

  <form id="rating" action"rating.php" method="post">
   <input type="hidden" name="id" id="idd" value="<?php $idprod?>" />
    <div class="vote">
     <label>
      <input id="stars" class="radioo" type="radio" name="fb" value="1" />
      <i class="fa" id="fa"></i>
     </label>
     <label>
      <input id="stars" class="radioo" type="radio" name="fb" value="2" />
      <i class="fa" id="fa"></i>
     </label>
     <label>
      <input id="stars" class="radioo" type="radio" name="fb" value="3" />
      <i class="fa" id="fa"></i>
     </label>
     <label>
      <input id="stars" class="radioo" type="radio" name="fb" value="4" />
      <i class="fa" id="fa"></i>
     </label>
     <label>
      <input id="stars" class="radioo" type="radio" name="fb" value="5" />
      <i class="fa" id="fa"></i>
     </label>
   </div>

Javascript (jQuery)

$('.vote label i.fa').on('click mouseover',function(){
    // remove classe ativa de todas as estrelas
    $('.vote label i.fa').removeClass('active');
    // pegar o valor do input da estrela clicada
    var val = $(this).prev('input').val();
    //percorrer todas as estrelas
    $('.vote label i.fa').each(function(){
        /* checar de o valor clicado é menor ou igual do input atual
        *  se sim, adicionar classe active
        */
        var $input = $(this).prev('input');
        if($input.val() <= val){
            $(this).addClass('active');
        }
    });
    $("#voto").html(val); // somente para teste
});
//Ao sair da div vote
$('.vote').mouseleave(function(){
    //pegar o valor clicado
    var val = $(this).find('input:checked').val();
    //se nenhum foi clicado remover classe de todos
    if(val == undefined ){
        $('.vote label i.fa').removeClass('active');
    } else { 
        //percorrer todas as estrelas
        $('.vote label i.fa').each(function(){
            /* Testar o input atual do laço com o valor clicado
            *  se maior, remover classe, senão adicionar classe
            */
            var $input = $(this).prev('input');
            if($input.val() > val){
                $(this).removeClass('active');
            } else {
                $(this).addClass('active');
            }
        });
    }
    $("#voto").html(val); // somente para teste
});

Query in PHP

$id = $_GET['cod'];
$sql = mysql_query("SELECT * FROM produtos WHERE id_produto = $id");
$test = mysql_query("SELECT votos, pontos FROM produtos WHERE id_produto = $id");
$aux = mysql_fetch_array($sql);
$idprod = $aux['id_produto'];
$row = mysql_fetch_array($test);
$voto = $row['votos'];
$ponto = $row['pontos'];
//saída do rating (pontuação)
$calc = round(($ponto/$voto),1);
  • What have you done about the code? Edit your question with the code, if possible post the name of the Javascript library you used.

  • jquery the library I will post the jquery here which makes type of a Hover with the enter mouse and mouse Leave

  • edited the question

  • PHP is still missing

  • I’m editing the php calm there

  • Your HTML code is broken, you better review it.

  • this example friend he only marks pro click the stars and also not put to fill with the votes in mine is all ready he scores with the color gold when you fill 5 stars but now I want him to take the result of the division and fill the stars according with that for her to have the note

  • http://plugins.krajee.com/star-rating/demo has a lot here.

  • You could for example use the Math.round() to round your number and mark the corresponding star

  • think with me not every division will return a whole number so I preferred to use the round there with that,1 I get 1 house after the comma

  • kind of like I can make a progress bar that’s hidden behind the icons and when it picks up the variable Calc and has a result this variable it makes like a progress bar behind the icons giving a fill effect on the stars as I can do this?

Show 7 more comments

1 answer

1

The point is that you have a note from 1 to 5, which all indicates, in this sense, if the note is 3, you would have to make a checkbox in input number 3:

<input id="stars" class="radioo" type="radio" name="fb" value="3"<?php echo ($calc == 3) ? ' checked="checked"' : '';?>/>

jQuery will do the onload event, based on this element, by assigning an active to the class from which there is a computed input value: going through the classes: .vote label i.fa:

Seria:<i class="fa active" id="fa"></i>

So, I believe there’s no need to keep it active manually, but if not, just do the same:

 <i class="fa<?php echo ($calc == 3) ? ' active' : '';?>" id="fa"></i>
  • What I’m seeing in this code wouldn’t look the way I wanted it and the numbers broken? How do I make example 4.8 it will only fill me 4 stars? it would not be easier to make a full type class with a yellow background and it would be shown according to the calculations?

  • or kind to play a color on fa same but she has to fill out according not only whole numbers what I want to do

  • star rating will always use integer number, to make other decimal scales, use percentage bar. What you can do is distribute your score between these numbers from 1 to 5. something like: if ($nota <= 1 ) { $rating = 1 } elseif( $nota <= 2 && $nota > 1 ) { $rating = 2} ...

  • but my score is not 1 to 5? if I have a score 10 on a certain product then I have 3 people who voted I won’t have to score/people? in that will give a number will give 3.3 with my example but we will assume that of 3.6 I will give 3 stars in that product I will ignore the . 6?

  • so if... 1 to 5 = 1 to 10

  • For example, if you have notes of 0-10, vc would have a group of 0 (no star), 1-2 (1 star), 3-4 (2 stars), 5-6(3 stars),7-8(4 stars),9-10(5 stars).

  • not face the notes vary they will increase in the example I did with 10 but if someone else give 3 stars to her she will increase the score goes to 13 and the number of votes 4

  • So if the number is broken, use jRate.

  • It has this option also, it fills only part of the stars: https://rateit.codeplex.com/ look at the example: http://www.radioactivethinking.com/rateit/example/example.htm

Show 4 more comments

Browser other questions tagged

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