PHP Comments System with Like Unlike

Many social media websites like Facebook have the options to add like unlike for the posts, links, photos added by the user. Adding likes and unlikes using AJAX will give a good user experience. It can be used effectively to filter and sort content. Sometimes, it can also be used to identify weeds out of the comments.

This comments system allows the user to add comments and to add replies to a particular comment added by some other user. Each comment and reply, it contains thumb icon that is shown to the user to add likes for the comments and replies. Initially, all the icons are in grey will represent that the comments and the replies have not yet been liked by the user. Once the user liked a comment by clicking the grey icon, then it turns to blue to update the status and the like count will also be updated.

List Comments with Like Unlike Status and Count

This code is for getting the comments list and its likes from the database. Each comment and reply row shows the like, unlike added by the current user. Also, it shows the total likes count of each record. The code is,

Like Unlike Comments via jQuery AJAX

While adding or unsetting the likes, an AJAX call is sent to the PHP to update the like status in the database. I have created tbl_like_unlike to store the comments like, unlike status. In this table, the likes and the unlikes are stored as 1, -1 respectively. The likes, unlikes are stored with the reference of the corresponding comment_id for which the user added the likes.