-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlikes.php
40 lines (28 loc) · 1.28 KB
/
likes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
include "includes/config.php";
$userid = 1;
$comid = $_POST['id'];
$type = $_POST['type'];
// Check entry within table
$query = "SELECT COUNT(*) AS cntpost FROM tblcomment_likes WHERE commentid=".$comid." and userid=".$userid;
$result = mysqli_query($con,$query);
$fetchdata = mysqli_fetch_array($result);
$count = $fetchdata['cntpost'];
if($count == 0){
$insertquery = "INSERT INTO tblcomment_likes(userid,commentid,type) values(".$userid.",".$comid.",".$type.")";
mysqli_query($con,$insertquery);
}else {
$updatequery = "UPDATE tblcomment_likes SET type=" . $type . " where userid=" . $userid . " and commentid=" . $comid;
mysqli_query($con,$updatequery);
}
// count numbers of like and unlike in post
$query = "SELECT COUNT(*) AS cntLike FROM tblcomment_likes WHERE type=1 and commentid=".$comid;
$result = mysqli_query($con,$query);
$fetchlikes = mysqli_fetch_array($result);
$totalLikes = $fetchlikes['cntLike'];
$query = "SELECT COUNT(*) AS cntUnlike FROM tblcomment_likes WHERE type=0 and commentid=".$comid;
$result = mysqli_query($con,$query);
$fetchunlikes = mysqli_fetch_array($result);
$totalUnlikes = $fetchunlikes['cntUnlike'];
$return_arr = array("likes"=>$totalLikes,"unlikes"=>$totalUnlikes);
echo json_encode($return_arr);