Asked  7 Months ago    Answers:  5   Viewed   27 times

I'm looking for examples of how to implement a StackOverflow / reddit voting system in php.

Basically I want the Up and Down arrow box. Are there any good examples out there?

 Answers

20

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

Wednesday, March 31, 2021
 
codingb
answered 7 Months ago
76

I ended up building my own implementation:

http://webspirited.com/tagit/index.php?themeroller=true#demo8

Wednesday, March 31, 2021
 
e_i_pi
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
78

A better way would be to have a different table to store Votes. And that table will have two attributes (VoterId, CandidateId) And you can fetch the Vote Count if you are allowing multiple votes from this table..

But it would be better to make VoterId a Primary key in this table.. To avoid multiple voting

CandidateType: - (TypeId(PK), typeName, maxVotePerVoterForThisType)

Voter Table: - (voterId(PK), voterName, otherInfo)

Candidate Table: - (candidateId(PK), candidateName, constituency, otherInfo, TypeId(FK))

Votes:- (voterId(PK, FK), TypeId(PK, FK), candidateId(FK))

*EDIT:- Schema edited with changed requirement in original post

*EDIT: - Added a field in CandidateType table to allow multiple votes.(E.g.: Now a voter can vote for 10 Senators, if maxVotePerVoter for this type is set to 10..)

Saturday, May 29, 2021
 
CMOS
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :