如何对投票系统进行编程? [英] How to program a vote up system?

查看:137
本文介绍了如何对投票系统进行编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正处于自学php的初期. 我正在给自己微型项目以推动自己.

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

到目前为止,我有一个通过php表单创建的MYSQL数据库.一栏是因果报应. 我将数据库表的值显示在html表中,并且在每一行的末尾,我想单击一个超链接,说一个加号,以将该行的业力级别提高1.然后加号会消失的.

Thus far I have a MYSQL database, created through a php form. One Column is for karma. I have the values of the database table display in an html table, and at the end of each row, I would like a click on a hyperlink, lets say a plus sign, to increase that row's karma level by 1. Then the plus sign would go away.

我应该每行都有一个自动递增整数作为主键.

I should that each row has an auto increment integer as a primary key.

推荐答案

在此示例中,假设您正在对此类答案进行投票.这将至少需要三个表:

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

用户答案投票

投票表将保存所有历史记录:

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)

在此示例中,我们看到记录了两个投票.用户12和28都对答案383进行了投票.用户12喜欢它,并在其支持中添加了1.用户28不喜欢它,因此从其支持中减去1.

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)

话虽如此,让我们看一下SO是如何做到这一点的:

当您点击向上箭头时,SO会向以下网址发出请求:

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

在此URL中,我们可以看到对帖子#1303528进行了投票.投票类型用2表示.这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

vote.php页面的代码类似于以下内容:

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

(警告:请勿按原样使用此代码.这只是示例,而不是解决方案)

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.";
    }
  }

}

这篇关于如何对投票系统进行编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆