本文介绍了喜欢和不喜欢系统的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加类似于Facebook的类似/不喜欢的系统,到目前为止,我已经将类似/不喜欢的列设置为文本类型。这样,我可以为喜欢/不喜欢的帖子的用户添加ID。那会是最好的办法吗?另外,除了上述问题之外,我如何阻止用户再次按下类似的按钮,再次不喜欢按钮?既然,一旦用户喜欢帖子,它应该显示不同的/不相似的选项?一个概念/想法将是很好的如何做到这一点。

解决方案

虽然很难做出扶手椅的决定,这里是我的想法:



首先,您可以为每个帖子设置一个喜欢的整数列。当用户单击上下移动时,该数字增加或减少。这不会对用户点击多次提供任何保护,但它很容易和快速。



另一种方式是拥有一个Like表,其中列为post_id,user_id和得分了。分数可以有两个值:'1'或'-1'。所有3列都是整数。当用户点击喜欢时,您可以在该行上使用一个INSERT / UPDATE命令,其中user_id& post_id匹配。



然后,要查看帖子的最后得分,您可以从该_table WHERE post_id =?中执行SELECT SUM(score)
$ b

使用第二种方法,如果您想查看最新的点击者的名称,可以添加时间戳列并搜索最近的条目。


I want to include a like/dislike system similar to Facebook and so far, I have set the like/dislike columns as a 'text' type. This is so that I can add the id for the user(s) who liked/disliked a post. Would that be the best way of doing it? Also, in addition to the question above, how would I stop a user pressing the like and dislike button again? Since, once a user has liked a post, it should display an unlike/undislike option? A concept/idea would be great of how to do this.

解决方案

While it's hard to make armchair decisions, here are my ideas:

First, you could have a 'likes' integer column for each post. When a user clicks up and down, have that number increment or decrement. This offers no protection against users clicking multiple times, but it's easy and fast.

Another way would be to have a 'Like' table, with columns post_id, user_id, and score. score can have two values: '1' or '-1'. All 3 columns are integers. When the user clicks 'like', you do an INSERT/UPDATE command on the row with user_id & post_id matching.

Then, to see the final score for a post, you do a SELECT SUM(score) FROM that_table WHERE post_id = ?.

With this second method, if you wanted to see the name of the most recent clicker, you could add a timestamp column and search for the most recent entry.

这篇关于喜欢和不喜欢系统的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:34