在PHP和MySQL评级体系 [英] Rating System in PHP and MySQL

查看:125
本文介绍了在PHP和MySQL评级体系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们看一下计算器网站,我们有票。但问题是什么是存储谁投了赞成票,谁已经不是佳豪。让我们也简化甚至更多,说我们只能投了,我们只能删除向上投票。

If we look at the stackoverflow website we have votes. But the question is what is the bestway to store who has voted and who has not. Lets also simplify this even more and say that we can only vote Up, and we can only Remove the Up vote.

我在想有该表是在这样的形式

I was thinking having the table to be in such form

的问题 - ID(INT)|用户id(INT)|标题(文本)|票(INT)| ratedBy(文本)

THRE剩下的就是自我explanitory但ratedBy是用户的一个逗号分隔的ID值。

Thre rest is self explanitory but ratedBy is a Comma Seperated Id values of the Users.

我想读 ratedBy ,并用它在用户id登录用户当前的进行比较。如果他dosent在 ratedBy 他可以投票了起来,否则他可以去除他的投票存在。这反过来将 ratedBy

I was thinking to read the ratedBy and compare it with the userId of the current logged in User. If he dosent exist in the ratedBy he can vote Up, otherwise he can remove his vote. Which in turn will remove the value from ratedBy

推荐答案

我想再拍表投票为好。用户和票之间的关系为n至n,因此一个新的表应该创建。它应该是这样的:

I think to make another table "vote" is better. The relationship between users and votes is n to n, therefore a new table should be created. It should be something like this:

question id (int) | user id (int) | permanent (bool) | timestamp (datetime)

常驻字段可以被用来使票停留给定的时间后,以SO一样。

其他字段可以根据期望的特征来添加。
由于每个行至少需要16B,您可以在桌上高达250M行的表使用4GB(FAT32的限制,如果有每桌一个存档,这对于MyISAM和InnoDB的情况下)之前。

此外,由于马修Scharley在评论中指出的那样,不要一次加载所有票到内存中(如获取所有表中一个结果)。您可以随时使用LIMIT子句来缩小查询结果。

Permanent field can be used to make votes stay after a given time, as SO does.
Other fields may be added according to desired features. As each row will take at least 16B, you can have up to 250M rows in the table before the table uses 4GB (fat32 limit if there is one archive per table, which is the case for MyISAM and InnoDB).
Also, as Matthew Scharley points out in a comment, don't load all votes at once into memory (as fetching all the table in a resultset). You can always use LIMIT clause to narrow your query results.

这篇关于在PHP和MySQL评级体系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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