|
| 1 | +<?php |
| 2 | + |
| 3 | +session_start(); |
| 4 | + |
| 5 | +include 'dbh.php'; |
| 6 | + |
| 7 | +if(isset($_SESSION['id'])){ |
| 8 | + //this code segment will be executed if the user has logged in |
| 9 | + $userId=mysqli_real_escape_string($conn,$_SESSION['id']); |
| 10 | + $username=mysqli_real_escape_string($conn,$_SESSION['username']); |
| 11 | + $likes=mysqli_real_escape_string($conn,$_POST['numberOfLikes']);//incrementing number of likes |
| 12 | + $id=mysqli_real_escape_string($conn,$_POST['id']);//the answer reply id |
| 13 | + $flag=true;//flag to see if user has already liked a reply |
| 14 | + $flag1=true;//flag to see if the user is liking his own reply |
| 15 | + |
| 16 | + //checking if the person who has posted the reply himself is trying to like it |
| 17 | + $sql3= "SELECT replyByUserId FROM answerrepliestable WHERE id='$id'"; |
| 18 | + $result3=mysqli_query($conn,$sql3); |
| 19 | + $row3=mysqli_fetch_assoc($result3); |
| 20 | + if($row3['replyByUserId']==$userId){//searching for userId instead of user name is very efficient(comparing integers is actually super fast compared to searching strings) |
| 21 | + //if user is trying to like his own reply |
| 22 | + //$flag1=false; |
| 23 | + echo 'user reply';//it is the user's own reply that he is trying to like |
| 24 | + } |
| 25 | + else{ |
| 26 | + //it is not his own reply |
| 27 | + $uid=$row3['replyByUserId'];//jyachya reply la like kela zatay tyacha id uid ahe |
| 28 | + //now checking if the user is liking a reply that he already has liked before |
| 29 | + |
| 30 | + $sql1= "SELECT likedByUserId FROM answerreplylikestable WHERE likedByuserId='$userId' AND replyId='$id'"; |
| 31 | + $result1=mysqli_query($conn,$sql1); |
| 32 | + if($row=mysqli_fetch_assoc($result1)){ |
| 33 | + //this means if the user has already liked the comment |
| 34 | + $likes=$likes-1; |
| 35 | + |
| 36 | + $sql="UPDATE answerrepliestable SET likes='$likes' WHERE id='$id'";//updating database |
| 37 | + $result=mysqli_query($conn,$sql); |
| 38 | + |
| 39 | + //deleting meme liking info into likestable |
| 40 | + $sql2="DELETE FROM answerreplylikestable WHERE replyId='$id'"; |
| 41 | + $result2=mysqli_query($conn,$sql2); |
| 42 | + |
| 43 | + //decreasing 0.5 point of the replier on account of the undo-like |
| 44 | + $sql3="UPDATE memberstable SET points=points-0.5 WHERE id='$uid'"; |
| 45 | + $result3=mysqli_query($conn,$sql3); |
| 46 | + |
| 47 | + //removing notification from uploader's feed |
| 48 | + $sql4="DELETE FROM notifications_table WHERE receiverId='$uid' AND senderId='$userId' AND notificationType='answerReplyLike' AND notificationForEventId='$id'"; |
| 49 | + $result4=mysqli_query($conn,$sql4); |
| 50 | + |
| 51 | + //echo 'undo like'; |
| 52 | + echo htmlentities($likes); |
| 53 | + } |
| 54 | + else{ |
| 55 | + //user has not already liked the reply |
| 56 | + |
| 57 | + //here, is the reply liking actual code |
| 58 | + $likes=$likes+1; |
| 59 | + $sql="UPDATE answerrepliestable SET likes='$likes' WHERE id='$id'";//updating database |
| 60 | + $result=mysqli_query($conn,$sql); |
| 61 | + |
| 62 | + //inserting reply liking info into replylikestable |
| 63 | + $sql2="INSERT INTO answerreplylikestable (replyId, likedByUserId) VALUES ('$id','$userId')"; |
| 64 | + $result2=mysqli_query($conn,$sql2); |
| 65 | + |
| 66 | + //increasing 0.5 point of the replier on account of the undo-like |
| 67 | + $sql3="UPDATE memberstable SET points=points+0.5 WHERE id='$uid'"; |
| 68 | + $result3=mysqli_query($conn,$sql3); |
| 69 | + |
| 70 | + //updating notification |
| 71 | + date_default_timezone_set('Asia/Kolkata');//setting the timezone |
| 72 | + $datetime=date('Y-m-d H:i:s'); |
| 73 | + |
| 74 | + $sql01="SELECT username FROM memberstable WHERE id='$userId'";//getting notification sender's username |
| 75 | + $result01=mysqli_query($conn,$sql01); |
| 76 | + $row01=mysqli_fetch_assoc($result01); |
| 77 | + $senderUsername=$row01['username']; |
| 78 | + |
| 79 | + $sql02="SELECT reply,replyToAnswerId FROM answerrepliestable WHERE id='$id'"; |
| 80 | + $result02=mysqli_query($conn,$sql02); |
| 81 | + $row02=mysqli_fetch_assoc($result02); |
| 82 | + $reply=$row02['reply']; |
| 83 | + $answerId=$row02['replyToAnswerId']; |
| 84 | + |
| 85 | + $sql03="SELECT answerForQuestionId FROM answerstable WHERE id='$answerId'"; |
| 86 | + $result03=mysqli_query($conn,$sql03); |
| 87 | + $row03=mysqli_fetch_assoc($result03); |
| 88 | + $questionId=$row03['answerForQuestionId']; |
| 89 | + |
| 90 | + $replyForNotification=(strlen($reply) > 13) ? substr($reply,0,10).'...' : $reply; |
| 91 | + $replydivname="answerreply_div".$id; |
| 92 | + |
| 93 | + $notificationString=$senderUsername.' liked your reply "'.$replyForNotification.'"'; |
| 94 | + $notificationType="answerReplyLike"; |
| 95 | + $notificationLink='questiondisplay.php?id='.$questionId.'#'.$replydivname; |
| 96 | + |
| 97 | + $sql4="INSERT INTO notifications_table (senderId,receiverId,notificationType,notification,notificationLink,viewingStatus,datetime,notificationForEventId) VALUES ('$userId','$uid','$notificationType','$notificationString','$notificationLink',0,'$datetime','$id')"; |
| 98 | + $result4=mysqli_query($conn,$sql4); |
| 99 | + |
| 100 | + echo htmlentities($likes);//sending back info to questiondisplay.php |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +} |
| 105 | +else{ |
| 106 | + echo 'login error'; |
| 107 | +} |
| 108 | + |
0 commit comments