Stack Overflow / reddit voting system in php
There are lots of scripts out there but it's not too hard to do yourself.
I've used jQuery (to handle AJAX) and a small PHP script before. For example, some pseudo-code:
// Some checking for recent votes from this user is appropriate here
if (isset($_POST['voteType'], $_POST['postId']) && $user->loggedIn) {
// insert vote into database if not already inserted
echo json_encode(array('error' => false));
} else {
// bad request/hack attempt
echo json_encode(array('error' => true, 'message' => 'Bad parameters sent'));
}
and then some jQuery:$('#upVote').click(function() {
$.post('vote.php', {voteType: 'up', postId: 42}, 'updateIcon(data, textStatus)', 'json');
});
function updateIcon(data, textStatus) {
// If error = false highlight the upvote icon
// else show the error message returned
}
jQuery.post Reddit-style voting system, how do I avoid duplicate votes?
You will need an extra table posts_vote
or something. Put the fields user_id and post_id in it. If a user votes a post up, you insert both IDs in this table. If a user votes down, find the record and delete it.
Trouble passing the right IDs and updating score in reddit-style voting-system
You'll need to pass $dbc
in as a function parameter as $dbc
is not declared inside the function's local variable scope:
function getAllVotes($id, $dbc){
/**
Returns an array whose first element is votes_up and the second one is votes_down
**/
$votes = array();
$q = "SELECT * FROM rating WHERE bidragId = $bidragId";
/** LINE 14 **/
$r = mysqli_query($dbc, $q);
/** LINE 15 **/
if(mysqli_num_rows($r)==1)//id found in the table{
$row = mysqli_fetch_assoc($r);
$votes[0] = $row['plus'];
$votes[1] = $row['minus'];
}
return $votes;
}
And when you're calling it:$cur_votes = getAllVotes($id, $dbc);
Make sure that you read up on variable scope in PHP as it should clear up a few things for you. PHP / MYSQL Voting system for user blog/forum
I believe that the best way to do it is to make a separate table for up/down votes, where you save user's id and is it up or down vote, and include reputation to comments/posts/whatever table.
If a user tries to do up/down vote you only need to check one comment (why 30?). Then you simply check in the separate table if the user has already voted for that comment/post. If so, you can remove vote (for example like in SO – if you press up vote twice, your upvote will be removed), or if person pressed a different button (f.e. downvote after upvote) you can just change his vote. The reason why I offered you to add reputation column to your posts/comments table and update it after all up,down votes is that then you will not need to get count(*)
of reputation from that separate table and your comment/post reputation will be easy and fast to recieve, especially, if your database has many rows.
What is the best approach to preventing voting spam? i.e. how does reddit do it
On my site I allow members to rate some code between 1 and 5 once every 24 hours. I stop spam by adding a hash code and using ajax to submit a vote. I haven't gotten any spam votes since I added it in June 2012. I used to get about 40 votes per day from bots, but now that has been eliminated for now.
For example this is what my link looks like:
http://phpsnips.com/process/vote.php?id=43&vote=3&enc=a560e4320af4e13e4170947cd42de18b
With that, enc is different every time the page loads, and once the vote is submitted it is changed again.
My steps
- Use a session to create an md5 hash
- place the hash on the link or form hidden field
- On the submission page test to make sure the session matches the link/field
- Reset the hash
- Redirect back to the original page (if not using ajax)
Best practice for comment voting database structure
To make sure that each voter votes only once, design your Votes table with these fields—CommentID, UserID, VoteValue. Make CommentID and UserID the primary key, which will make sure that one user gets only one vote. Then, to query the votes for a comment, do something like this:
SELECT SUM(VoteValue)
FROM Votes
WHERE CommentID = ?
Does that help? Real Time Online Voting Without Leaving Page?
Read up on Ajax. Essentially, you'd be using Javascript to handle the form submissions and get back responses in the background, then modifying the page based on that.
Voting system in php and mysql for the state votes, like president elecctions
SELECT distinct id,nombre, apellido1,apellido2, personas.seccion from personas
LEFT JOIN votaciones on personas.id = votaciones.id_persona
WHERE personas.seccion = 1 AND mesa = 'A'
AND (id_eleccion IS NULL OR id
NOT IN (SELECT id FROM personas JOIN votaciones ON personas.id = votaciones.id_persona WHERE id_eleccion =1))
That is the conclusion i came to.
Related Topics
PHP Require File from Top Directory
Which PHP Mcrypt Cipher Is Safest
How to Use a PHP Class from Another File
How to Add Item to the JSON File Formatted Array
How to Split a String into an Array of Unicode Characters in PHP
Pdo_Sqlite Driver Not Present.. What to Do
File_Get_Contents() Failed to Open Stream:
How to Force a Certain Tls Version in a PHP Stream Context for the Ssl:// Transport
How to Display HTML to the Browser Incrementally Over a Long Period of Time
Change Xml Node Element Value in PHP and Save File
PHP - Swiftmailer Using Starttls and Self Signed Certificates
Denormalize Nested Structure in Objects with Symfony 2 Serializer
Imagemagick Brew Installation with PHP Module in MAC Os X
PHP Splitting an Array into Two Arrays - Keys Array and Values Array
Are There Any PHP Docblock Parser Tools Available
Remove "(Optional)" Text from Checkout Fields in Woocommerce 3.4+
How to Keep a PHP Session Active Even If the Browser Is Closed