[英]What is the right way of checking if the user has voted or not?
For example, 10 results are displayed from MySQL table products
and each product has a like and dislike button. 例如,从MySQL表
products
显示10个结果,每个产品都有一个“喜欢”和“不喜欢”按钮。 If the user presses like or dislike, the voted
table is updated. 如果用户按下喜欢或不喜欢,则
voted
表将更新。
Now what i'm having trouble with is: What is the right way of checking if the user has voted or not? 现在,我遇到的问题是:检查用户是否投票的正确方法是什么? What to store If product has been voted with like or dislike?
存储什么如果产品已被喜欢或不喜欢投票?
If there is 300 results, I don't want to do 300 queries to check in the voted
table if the productid
and userid
is present. 如果有300个结果,我不想进行300个查询来检查带
voted
表中是否存在productid
和userid
。 What would be the correct way of doing this? 正确的做法是什么? I still want to show the results, just not the like or dislike button if the user has voted.
我仍然想显示结果,如果用户已投票,则不会显示“喜欢”或“不喜欢”按钮。
Table A stores products, eg 表A存储产品,例如
id | product |
Table B stores user votes, eg 表B存储用户投票,例如
id | user_id | product_id | vote (like/dislike)
Then you can do a simple join query 然后您可以做一个简单的联接查询
SELECT `A`.`id`, `A`.`product`, `B`.`vote` from `A` LEFT JOIN `B` on `A`.`id` = `B`.`product_id` WHERE `B`.`user_id` = $current_user_id_value;
Then the result set will look like this 然后结果集将如下所示
id | product | vote |
1 | "product 1" | NULL |
2 | "product 2" | like |
3 | "product 3" | dislike |
4 | "product 4" | NULL |
...
使用左外部联接添加投票表中的行,其中用户ID与当前用户匹配,产品ID与产品表中的产品ID匹配到结果集。
The easiest is setting a cookie for the user and the products they have voted. 最简单的方法是为用户及其投票的产品设置Cookie。 Of course, the user could just delete their cookies and vote again.
当然,用户可以只删除其Cookie并再次投票。
The consistent way would be the one you describe, saving a row for each productId
and userId
. 一致的方式将是您描述的方式,为每个
productId
和userId
保存一行。 No need to query multiple times, though, you'd just have to do SELECT count(productID) WHERE productId=1 and userId=42
不过,无需多次查询,您只需要在
SELECT count(productID) WHERE productId=1 and userId=42
执行SELECT count(productID) WHERE productId=1 and userId=42
You can choose, amongst different scenarios, between consistency or performance. 您可以在不同方案之间选择一致性或性能。 Maybe another database engine would be the best choice, together with cookies.
也许与cookie一起,另一个数据库引擎将是最佳选择。
I have an odd solution :) 我有一个奇怪的解决方案:)
votes
, type blob (or text) votes
的字段,键入blob(或文本) Save the voted user in an array like: 将投票的用户保存在数组中,例如:
$votes = array( 1 => true, // userId => like/dislike (true/false) );
Use serialize
& unserialize
with object $votes
(field vote
) to load or save voted users to you database. 对对象
$votes
( unserialize
vote
)使用serialize
和反unserialize
将已投票的用户加载或保存到您的数据库。 With less than 1 thounsand users votes on a product, the votes
field is as small as acceptable I guess :) 少于1名thounsand用户对某产品进行
votes
, votes
字段的范围很小,我猜是:)
Then you can check if the user is voted by isset($votes[$userId])
and like or dislike by: 然后,您可以检查用户是否被
isset($votes[$userId])
投票,并通过以下方式投票:
if (isset($votes[$userId])) if ($votes[$userId] == true) echo 'like'; else echo 'dislike';
Because you do the checking by PHP so execute time doesn't matter. 因为您通过PHP执行检查,所以执行时间无关紧要。
Hope this helps! 希望这可以帮助!
you can use memcache, to have the values stored with you as key=>value pair, 您可以使用内存缓存,将值作为key => value对存储在一起,
key can be "product_id+user_id" and when ever any change happens, update the db as well as memcache. 密钥可以是“ product_id + user_id”,并且无论何时发生任何更改,都要更新数据库以及内存缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.