I found this script from internet search by using like / dislike button for my script
http://wcetdesigns.com/view.php?dtype=tutorials&category=php&id=22
Everythings work good when using for single post but,
I want to use this rating script for all documents come from while cycle
For example, I have news script and all news are listed, and near to title you see like/dislike buttons. (like stackoverflow web page) and I want to rate all this content separately. In this script, I couldn't separate article id's in same page
Can you please help me about that?
First, you need to specify for each button which article is it for, you can easily do it with data- attribute
<button class="votebutton" data-article="1" data-vote="1">Up vote</button>
<button class="votebutton" data-article="1" data-vote="-1">Down vote</button>
<span class="votes" data-article="1"><?=//print the current number of votes of this article//?>
Now you need to do something when the button is clicked, so your javascript would look something like that:
$('.votebutton').on("click", function(){
vote = $(this).attr("data-vote");
article = $(this).attr("data-article");
rateArticle(vote, article);
})
now you need the rateArticle() function that would look something like this:
function rateArticle(vote, article){
var data = 'vote='+rating+'&articleID='+article;
$.ajax({
type: 'POST',
url: 'rateArticle.php',
data: data,
success: function(votes){
$('.vote[data-article='+article+']').html(votes);
}
});
}
on the server side, the rateArticle.php file that you'll post to would look something like this:
$vote = $_POST['vote'];
$articleID = $_POST['articleID'];
$currentVotes = ; //fill it with the votes that the article currently have
if($vote != "-1" || vote != "1"){
//goddamn script kids messing with us again, let's put them back where they belong
header("Location: http://www.lemonparty.com"); //probably not this, but you can think of something
}
if(userHasAlreadyVoted()){ //if user has already voted we just return the current number of votes
echo $currentVotes;
}else{
$newVotes = $currentVotes + $vote;
//save newVotes to db...
echo $newVotes;
}
And that's about it...