How to program a vote up system?

前端 未结 2 765
情歌与酒
情歌与酒 2021-02-11 02:55

I am in the very beginnings of teaching myself php. I am giving myself micro projects to push myself.

Thus far I have a MYSQL database, created through a php form. One C

2条回答
  •  醉酒成梦
    2021-02-11 03:23

    For this example, let's assume you're voting on so-answers. This will require at least three tables:

    Users, Answers, Votes

    The votes table will hold all the history:

    voteid | userid | answerid | value
    ----------------------------------
       1   |   12   |   383    |   1
       2   |   28   |   383    |  -1  (negative number would require signed values)
    

    In this example, we see that two votes have been recorded. Users 12 and 28 both voted on Answer 383. User 12 loved it, and added 1 to its support. User 28 didn't like it, and subtracted 1 from its support.

    Anytime a user votes, you should first check to see if that user has already voted on that particular question. If they have, you can reject any further attempts to vote, or overwrite their old vote with a new one.

    SELECT * 
    FROM votes 
    WHERE (userid = 12) 
      AND (answerid = 383)
    

    That's a very simple example query that will tell you if the user has already voted or not. If it returns records, you know they've voted. You can respond with a very nice "Sorry, you've already voted." message, or you can overwrite it:

    UPDATE votes 
    SET value = $votevalue 
    WHERE (userid = 12) 
      AND (answerid = 383)
    

    Having said that, let's look at how SO accomplishes this:

    When you click the up-vote arrow, SO fires a request off to a url like the following:

        http://stackoverflow.com/posts/1303528/vote/2

    In this URL, we can see the vote is being cast on post #1303528. And the vote-type is represented by 2. This 2 likely represents "add one." You could use a more basic url, and go with something like:

        vote.php?answerid=383&vote=1

    The vote.php page would have code similar to the following:

    (warning: do not use this code as-is. It is meant to be an example, not a solution)

    if ($_GET) {
    
      $userid   = $_SESSION["userid"]; // assumes the user is logged in via SESSIONS
      $answerid = $_GET["answerid"];
      $votetype = $_GET["vote"];
    
      $query = "SELECT * 
                FROM votes 
                WHERE (userid = {$userid}) 
                  AND (answerid = {$answerid})";
    
      $result = mysql_query($query) or die(mysql_error());
    
      if (mysql_num_rows($result) > 0) {
        # User has voted
        print "Sorry, you are only allowed one vote.";
      } else {
        # User has not voted, cast the vote
        $query = "INSERT INTO votes (userid, answerid, votevalue)
                  VALUES({$userid},{$answerid},{$vote})";
        $result = mysql_query($query) or die(mysql_error());
        if (mysql_affected_rows($result) > 0) {
          print "Your vote has been recorded.";
        }
      }
    
    }
    

提交回复
热议问题