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?



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

Wednesday, March 31, 2021
answered 7 Months ago

I ended up building my own implementation:

Wednesday, March 31, 2021
answered 7 Months ago

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:

Related usability ticket:

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

Wednesday, March 31, 2021
answered 7 Months ago

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
answered 5 Months ago

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
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 :