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.
– Giancarlo Abel Giulian
jquery the library I will post the jquery here which makes type of a Hover with the enter mouse and mouse Leave
– Leonardo Costa
edited the question
– Leonardo Costa
PHP is still missing
– Giancarlo Abel Giulian
I’m editing the php calm there
– Leonardo Costa
related: Star voting with radio input, javascript/css
– Pedro Sanção
Your HTML code is broken, you better review it.
– Ivan Ferrer
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
– Leonardo Costa
http://plugins.krajee.com/star-rating/demo has a lot here.
– Diego Souza
You could for example use the
Math.round()
to round your number and mark the corresponding star– Tafarel Chicotti
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
– Leonardo Costa
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?
– Leonardo Costa