4
I was able to do the star rating system, but I wanted the vote to be shown on the screen or stored on the star, so it would be filled according to the votes. Someone could help me, here’s my code:
That’s the page where the stars are:
<script>
$(document).ready(function () {
$(".vote #stars").click(function () {
$.post('rating.php',{rate:$(this).val()},function(d){
if(d>0)
{
alert('Seu voto Ja foi confirmado');
}else{
alert('Obrigado por Votar');
}
});
$(this).attr("checked");
});
});
</script>
<div class="vote">
<label>
<input id="stars" type="radio" name="fb" value="1" />
<i class="fa" id="fa"></i>
</label>
<label>
<input id="stars" type="radio" name="fb" value="2" />
<i class="fa" id="fa"></i>
</label>
<label>
<input id="stars" type="radio" name="fb" value="3" />
<i class="fa" id="fa"></i>
</label>
<label>
<input id="stars" type="radio" name="fb" value="4" />
<i class="fa" id="fa"></i>
</label>
<label>
<input id="stars" type="radio" name="fb" value="5" />
<i class="fa" id="fa"></i>
</label>
</div>
Here the script for the hover
in the stars and to mark them as actives
:
$('.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
});
And this is the PHP rating page. I wish the user could vote for it more than once, but I’m not getting it. Note: This part of the bank in PHP I did following a tutorial in English and I’m trying to make it not block the vote for only one person.
<?php
$ipaddress = md5($_SERVER['REMOTE_ADDR']); // here I am taking IP as UniqueID but you can have user_id from Database or SESSION
$servername = "localhost"; // Server details
$username = "root";
$password = "";
$dbname = "filmes";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Unable to connect Server: " . $conn->connect_error);
}
if (isset($_POST['rate']) && !empty($_POST['rate'])) {
$rate = $conn->real_escape_string($_POST['rate']);
// check if user has already rated
$sql = "SELECT `id` FROM `tbl_rating` WHERE `user_id`='" . $ipaddress . "'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if ($result->num_rows > 0) {
echo $row['id'];
} else {
$sql = "INSERT INTO `tbl_rating` ( `rate`, `user_id`) VALUES ('" . $rate . "', '" . $ipaddress . "'); ";
if (mysqli_query($conn, $sql)) {
echo "0";
}
}
}
$conn->close();
?>
A user can vote multiple times on the same content? Your doubt is in showing the user’s vote with the stars, or make the overall average?
– Papa Charlie
I need the general media to show what I want to do sorry if I wasn’t clear on the question but I want to do the general media and then retrieve the votes and show on the stars as the average vote that the article will have. and yes I want the user to be able to vote more than once
– Leonardo Costa
If I understand correctly, you will have a query with
AVG( rate )
to calculate the overall average, and a query with the list of all votes: ( user1 - , user2 - )...– Papa Charlie
how can I implement this in my project?
– Leonardo Costa
You just put the
INSERT
in the question, show theSELECT
where you retrieve the votes.– Papa Charlie
I did not select to recover but can I do it when I do select to recover I have to play where the value of the rating I recover? i dou select in the Where rate table and where do I play this value that was saved to show in html? but also have to do the calculation where implement?
– Leonardo Costa
Example:
select AVG( rate ) as rate from tbl_rating where id = 1
this query will return an index with the total average of votes.– Papa Charlie
amigo se Voce reparacao I have a select on the part in php that is catching the id of the user I already switched for him to get the field rate that and where are the votes and stayed like this: $sql = "SELECT
id
FROMtbl_rating
WHERErate
='" . $rate . "'"; now as I do the calculation and then call it in html to keep showing the amount of votes that article had?– Leonardo Costa
Managed to Resolve?
– durtto
Related: http://answall.com/q/89133/129
– Sergio
Let’s go by parts, so the user votes more than once removes the check that he has already voted:
$sql = "SELECT
idFROM
tbl_ratingWHERE
user_id='" . $ipaddress . "'";
 $result = $conn->query($sql);
 $row = $result->fetch_assoc();
 if ($result->num_rows > 0) {
 echo $row['id'];
 } else {
– Marcelo Bonus