$data = array();
$data = unserialize(file_get_contents('./ratings.data.txt'));
$data['votes'] += 1;
$data['points'] += $value;
$data['rate'] = round( $data['points'] / $data['votes'], 1 );
file_put_contents('./ratings.data.txt', serialize($data));
setcookie('user', md5($_SERVER['REMOTE_ADDR'].'lol'));
<?php
$data = array();
$data = unserialize(file_get_contents('./ratings.data.txt'));
$rate = $data['rate'];
$votes = $data['votes'];
?>
<div id="total">Голосов <?php echo $votes; ?></div>
<div id="rate">Рейтинг <?php echo $rate; ?> </div>
<div class="stars-contanet"></div>
<div class="stars-you"></div>
<style>
.star-proc {
background: #000;
height: 100%;
-webkit-transition: 0.5s;
-o-transition: 0.5s;
transition: 0.5s;
}
.stars-contanet {
position: relative;
width: 150px;
height: 30px;
}
.stars {
background: url('star.png') no-repeat;
float: left;
height: 30px;
width: 30px;
}
.star-active {
height: 100%;
background-color: yellow;
cursor:pointer;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
var star = '<div class="stars"></div>';
$('.stars-contanet').html(star+star+star+star+star+'<div class="star-proc"></div>');
$('.star-proc').width($('#rate').text().replace('Рейтинг', '')*20+'%');
if (!localStorage.getItem('rate')){
$('.stars').bind('click', getrate);
$('.stars').hover(
function() {
$(this).prevAll('.stars').andSelf().addClass('star-active').attr('title',$('.star-active').length);
},
function() {
$(this).prevAll('.stars').andSelf().removeClass('star-active');
}
);
} else {
$('.stars-you').text('Ваша оценка: '+localStorage.getItem('rate'));
}
function getrate() {
$('.stars').unbind();
var val = $('.star-active').length;
$('.stars').removeClass('star-active');
localStorage.setItem('rate', val);
$.post('rate.php',
{val : val},
function(d) {
$('.star-proc').width(d.rate*20+'%');
$('#total').text('голосов '+d.votes);
$('#rate').text('рейтинг '+d.rate);
},
'json'
).complete(function(){
$('.stars-you').text('Ваша оценка: '+val);
});
}
</script>
<?php
$data = array();
$data = unserialize(file_get_contents('./ratings.data.txt'));
preg_match('/([1-5]{1})/', $_POST['val'], $match);
$vote = $match[0];
$data['votes'] += 1;
$data['total_points'] += $vote;
$data['rate'] = round( $data['total_points'] / $data['votes'], 1 );
file_put_contents('./ratings.data.txt', serialize($data));
echo json_encode($data);
?>