Use PHP to develop this like function. How can you click like to record who likes it, and cancel the like when you click again? Novices seeking advice.
Use php to develop this like function. How can you click like to record who likes it, and cancel the like when you click again? Novices seeking advice.
That’s pretty much the answer above, I’ll just add:
There should be such a field in the article table, like_num
, dislike_num
These two fields record the number of people who like and dislike respectively, so that it is convenient to get the value (the likes and ratings of the topic post are almost the same);
The next step is almost the same as above. Likes and comments are recorded to determine whether the user has done this type of operation.
You should suggest a like table
The fields should at least include: article id, uid, islike
When the user clicks "Like", first check whether the user has clicked "Like" on this article. If so, It prompts the user to "already like it". If not, the relevant information is written into the table.
This kind of work can be done with NoSQL first, such as redis. If you click like it, it will be put into a queue of redis. If you cancel it, it will be removed from the queue. Finally, it will be written to the database uniformly in a certain period of time, which can achieve quick response.
The user likes this behavior and records it. The next time he clicks, it will be judged based on the retrieval whether the behavior is liked or unliked.
Article table aid content
Article user relationship table (rel_table) id aid userid like
User table userid
The user thinks article 1 is good, click like: Add 1 record to the article user relationship table
Cancel the like and mark the like as 0 (update rel_table set like=1 where aid=1 and userid=1190)
You can know this through the relationship table Whether the user likes this article, (select like from rel_table where aid=1 and userid=1190)
can also know which articles he likes (select * like from rel_table where userid=1190)
I just made such a function not long ago. The idea is as follows:
First create a user behavior record table, which mainly includes the user's id and the id of the liked article. There is also a status. When the user clicks, the status changes to 1. If you cancel, change it to 0. If you like it next time, change it to 1
Secondly, create an extra table for the article extend, which is used to record some data, such as likes, number of comments, etc., and then the user will perform an operation every time Just operate the corresponding data in the extend table, and then after opening the article, you can judge whether you like it based on whether the status can be found or the status of the status.