How to show the bank’s value in a star rating?

Asked

Viewed 1,055 times

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?

  • 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

  • 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 - )...

  • how can I implement this in my project?

  • You just put the INSERT in the question, show the SELECT where you retrieve the votes.

  • 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?

  • 1

    Example: select AVG( rate ) as rate from tbl_rating where id = 1 this query will return an index with the total average of votes.

  • 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 FROM tbl_rating WHERE rate='" . $rate . "'"; now as I do the calculation and then call it in html to keep showing the amount of votes that article had?

  • Managed to Resolve?

  • Related: http://answall.com/q/89133/129

  • Let’s go by parts, so the user votes more than once removes the check that he has already voted: $sql = "SELECT idFROMtbl_ratingWHEREuser_id='" . $ipaddress . "'";&#xA; $result = $conn->query($sql);&#xA; $row = $result->fetch_assoc();&#xA; if ($result->num_rows > 0) {&#xA; echo $row['id'];&#xA; } else {

Show 6 more comments

1 answer

-3

Friend does the following you want me to vote more than once no? If yes create a button where the user id will be sent to a table that will be tamed +1 to existing votes

input type="submit" name ="voto" value="<?php // PEGUE O ID DO USUARIO ?>"/

//vou fazer com PHP estrutural
$id =  $_POST['voto'];

$voltar = $volta($id);

function volta($id){

$sql = "SELECT FROM tbl_name WHERE id_user = '$id'"
$resul = pg_query($sua_conexao, $sql);
$voltar = pg_fetch_assoc($result);
return $voltar;

}

votar();

function votar($id){
global $voltar;
$nvoto = $voltar['voto'] + 1;
sql = "INSERT INTO nome_tabela(id_user,voto) values('$id','$voto')";
$resultar = pg_query($sua_conexao, $sql);
$pg_close($sua_conexao);

}

I did with Posts and did not test, but I hope you have understood the logic if you need in POO and PDO can redo in a nicer way

Browser other questions tagged

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