this system for star rating is working everything but I just wanted to create a cookie so that user votes only once every day I tried in several ways but the cookie is not generated
$(function(){
var average = $('.ratingAverage').attr('data-average');
function avaliacao(average){
average = (Number(average)*20);
$('.bg').css('width', 0);
$('.barra .bg').animate({width:average+'%'}, 500);
}
avaliacao(average);
$('.star').on('mouseover', function(){
var indexAtual = $('.star').index(this);
for(var i=0; i<= indexAtual; i++){
$('.star:eq('+i+')').addClass('full');
}
});
$('.star').on('mouseout', function(){
$('.star').removeClass('full');
});
$('.star').on('click', function(){
var idArticle = $('.article').attr('data-id');
var voto = $(this).attr('data-vote');
$.post('votar.php', {votar: 'sim', artigo: idArticle, ponto: voto}, function(retorno){
avaliacao(retorno.average);
$('.votos span').html(retorno.votos);
}, 'jSON');
});
});
.barra{width:150px; height:30px; background:#ebebeb; position:relative;}
.stars{position:absolute; left:0; top:0; width:100%;}
.star{
float:left;
width:30px;
height:30px;
text-align:center;
position:relative;
cursor:pointer;
}
.star.full{background:linear-gradient(to bottom, #fee24f, #f4bb2f)}
.bg{float:left;height:30px; width:30%; background:linear-gradient(to bottom, #fee24f, #f4bb2f);}
.starAbsolute{
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
background:url(../starpng.png) top left no-repeat;
background-size:cover;
}
<?php
include_once "../Config.inc.php";
?>
<html lang="pt-BR">
<head>
<meta charset=UTF-8>
<title>pagina teste</title>
<link href="<?= BASE; ?>/css/style.css" rel="stylesheet" type="text/css" />
<script src="<?= BASE; ?>js/jquery-3.1.1.min.js"></script>
<script src="<?= BASE; ?>/js/avaliations.js"></script>
</head>
<body>
<?php
$id_pro =1;
$pegaArtigo = $pdo->prepare("SELECT * FROM 'wc_app' WHERE id = $id_pro");
$pegaArtigo->execute(array($id_pro));
while($artigo = $pegaArtigo->fetchObject()){
$calculo = ($artigo->pontos == 0) ? 0 : round(($artigo->pontos/$artigo->votos), 1);
?>
<span class="ratingAverage" data-average="<?php echo $calculo;?>"></span>
<span class="article" data-id="<?php echo $id_pro;?>"></span>
<div class="barra">
<span class="bg"></span>
<span class="stars">
<?php for($i=1; $i<=5; $i++):?>
<span class="star" data-vote="<?php echo $i;?>">
<span class="starAbsolute"></span>
</span>
<?php
endfor;
echo '</span></div><p class="votos"><span>'.$artigo->votos.'</span> votos</p>';
}
?>
</body>
</html>
require 'environment.php';
global $config;
$config = array();
if(ENVIRONMENT == 'development') {
$config['dbname'] = 'megaki';
$config['host'] = 'localhost';
$config['dbuser'] = 'system';
$config['dbpass'] = 'system';
} else {
$config['dbname'] = 'megakico_megaki';
$config['host'] = 'localhost';
$config['dbuser'] = 'megakico_system';
$config['dbpass'] = 'system302573';
}
try {
$pdo = new PDO("mysql:dbname=".$config['dbname'].";host=".$config['host'], $config['dbuser'], $config['dbpass'], [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"]);
}catch(PDOException $e){
echo "conhexao ao banco de dados falhou".$e->getMessage();
}