-
Notifications
You must be signed in to change notification settings - Fork 0
/
like.php
125 lines (94 loc) · 3.19 KB
/
like.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
require "config/config.php";
include("includes/classes/User.php");
include("includes/classes/Post.php");
include("includes/classes/Notification.php");
// check if user is logged in
if (isset($_SESSION['username'])) {
$data = $_SESSION['logged_in_user'];
$userLoggedIn = $data['username'];
$first_name = $data['first_name'];
} else {
header("Location: register.php");
}
// post id sent by parent html element(post.php)
if (isset($_GET['post_id'])) {
$post_id = $_GET['post_id'];
}
// get existing total likes for the post
$get_likes = mysqli_query($con, "SELECT likes, added_by FROM posts WHERE id='$post_id'");
$row = mysqli_fetch_array($get_likes);
$total_likes = $row['likes'];
$user_liked = $row['added_by'];
// get existing total likes for the user
$user_details_query = mysqli_query($con, "SELECT * FROM users WHERE username='$user_liked'");
$row = mysqli_fetch_array($user_details_query);
$total_user_likes = $row['num_likes'];
// if like button is pressed
if(isset($_POST['like_button'])) {
// increment likes
$total_likes++;
$total_user_likes++;
// update data in db
$query = mysqli_query($con, "UPDATE posts SET likes='$total_likes' WHERE id='$post_id'");
$user_likes = mysqli_query($con, "UPDATE users SET num_likes='$total_user_likes' WHERE username='$user_liked'");
$insert_query = mysqli_query($con, "INSERT INTO likes (username, post_id) VALUES ('$userLoggedIn', '$post_id')");
// insert notification
// only when liking other users post
if ($user_liked != $userLoggedIn) {
$notification = new Notification($con, $userLoggedIn);
$notification->insertNotification($post_id, $user_liked, "like");
}
}
// if unlike button is pressed
if(isset($_POST['unlike_button'])) {
// decrement likes
$total_likes--;
$total_user_likes--;
// update data in db
$query = mysqli_query($con, "UPDATE posts SET likes='$total_likes' WHERE id='$post_id'");
$user_likes = mysqli_query($con, "UPDATE users SET num_likes='$total_user_likes' WHERE username='$user_liked'");
$insert_query = mysqli_query($con, "DELETE FROM likes WHERE username='$userLoggedIn' AND post_id='$post_id'");
}
// check for previous like
$check_query = mysqli_query($con, "SELECT * FROM likes WHERE username='$userLoggedIn' AND post_id='$post_id'");
$num_rows = mysqli_num_rows($check_query);
$post_id = trim($post_id);
// if previously liked, then will display dislike/unlike button
if($num_rows > 0) {
// unlike button
echo "<form action='like.php?post_id=$post_id' method='POST'>
<input type='submit' class='comment_like' name='unlike_button' value='Unlike'>
<div class='like_value'>
" . $total_likes . " Likes
</div>
</form>";
} else {
// like button
echo "<form action='like.php?post_id=$post_id' method='POST'>
<input type='submit' class='comment_like' name='like_button' value='Like'>
<div class='like_value'>
" . $total_likes . " Likes
</div>
</form>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- Custom CSS -->
<link rel="stylesheet" type="text/css" href="assets/css/style.css">
<style type="text/css">
body {
background-color: #fff;
margin-top: 3px;
}
* {
font-family: Arial, Helvetica, Sans-serif;
}
</style>
</head>
<body>
</body>
</html>