允许用户评价一次 PHP MySQL 的评论 [英] Allow users to rate a comment once PHP MySQL

查看:56
本文介绍了允许用户评价一次 PHP MySQL 的评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,用户可以在其中对页面上留下的评论进行评分.每条评论都有一个唯一的 ID(例如 402934)如果我希望用户能够点赞/不点赞评论,我可以看到我将如何制作一个简单的计数器代码来跟踪点赞数与拇指点数-downs 但我如何确保每个用户只对评论进行一次排名.我打算创建一个数据库,将每个评论编号作为一行,并在该行中包含一个数组,其中包含所有对它进行排名的用户竖起大拇指,而所有对其竖起大拇指的用户进行了排名,但我有一种感觉不是最好的办法.我的下一个想法是为每个用户创建一个表格,然后使用一个数组显示该用户已排名的所有评论.它可能会以这种方式运行得更快(例如,检查用户的 150 个排名与评论的 6050 个排名,但我仍然觉得有更好的方法......有什么想法吗?

I have a website where users can rate comments that are left on pages. Each comment has a unique ID (E.g. 402934) If I want users to be able to thumb-up/thumb-down said comments I can see how I would make a simple counter code to keep track of the number of thumb-ups vs thumb-downs but how can I make sure that each user only ranks said comment once. I was going to make a database with each comment number as a row and in that row having an array of all the users that have ranked it thumbs up and all the users that have ranked it thumbs down but I had a feeling that wasn't the best way. My next thought was to have a table for each user and then having an array showing all the comments said user has ranked. It would probably run faster this way (e.g. checking from a user's 150 rankings verse a comment's 6050 rankings but I still feel like there is a better way... any ideas?

推荐答案

user_idcomment_idvote TINYINT(1) 创建一个新表代码>.

Create a new table with user_id, comment_id and vote TINYINT(1).

vote1 的值是一个赞,vote0 的值是一个大拇指朝下.

A value of 1 in vote is a thumbs up, A value of 0 in vote is a thumbs down.

(comment_id, user_id) 有一个 UNIQUE KEY 约束.

Have a UNIQUE KEY constraint on (comment_id, user_id).

如果您按照上述操作,将很容易检查用户是否对特定评论进行了投票,如果您希望能够快速(如在快速执行中)查看用户所做的所有评论您还应该将 INDEX 添加到 user_id.

If you follow the above it will be easy to check whether a user has cast a vote on a specific comment, if you'd like to be able to quickly (as in fast execution) see all the comments a user has made you should also add an INDEX to user_id.

当用户投票时,您可以使用REPLACE INTOuser_comment_thumbs,如下所示:

When a user votes you could use REPLACE INTO to user_comment_thumbs, such as the below:

REPLACE INTO `user_comment_thumbs` (user_id,comment_id,vote)
VALUES (@user_id, @comment_id, @vote);

如果用户已经投票,表中的条目将被更新,否则将插入一个新行.

If the user has already made a vote the entry in the table will be updated, otherwise a new row will be inserted.

这篇关于允许用户评价一次 PHP MySQL 的评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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