-
Notifications
You must be signed in to change notification settings - Fork 0
/
like.php
105 lines (87 loc) · 2.86 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
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="assets/css/style.css">
</head>
<body>
<style type="text/css">
* {
font-family: Arial, Helvetica, Sans-serif;
}
body {
background-color: #fff;
}
form {
position: absolute;
top: 0;
}
</style>
<?php
require 'config/config.php';
include("includes/classes/User.php");
include("includes/classes/Post.php");
include("includes/classes/Notification.php");
if (isset($_SESSION['username'])) {
$userLoggedIn = $_SESSION['username'];
$user_details_query = mysqli_query($con, "SELECT * FROM users WHERE username='$userLoggedIn'");
$user = mysqli_fetch_array($user_details_query);
}
else {
header("Location: register.php");
}
//Get id of post
if(isset($_GET['post_id'])) {
$post_id = $_GET['post_id'];
}
$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'];
$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'];
//Like button
if(isset($_POST['like_button'])) {
$total_likes++;
$query = mysqli_query($con, "UPDATE posts SET likes='$total_likes' WHERE id='$post_id'");
$total_user_likes++;
$user_likes = mysqli_query($con, "UPDATE users SET num_likes='$total_user_likes' WHERE username='$user_liked'");
$insert_user = mysqli_query($con, "INSERT INTO likes VALUES('', '$userLoggedIn', '$post_id')");
//Insert Notification
if($user_liked != $userLoggedIn) {
$notification = new Notification($con, $userLoggedIn);
$notification->insertNotification($post_id, $user_liked, "like");
}
}
//Unlike button
if(isset($_POST['unlike_button'])) {
$total_likes--;
$query = mysqli_query($con, "UPDATE posts SET likes='$total_likes' WHERE id='$post_id'");
$total_user_likes--;
$user_likes = mysqli_query($con, "UPDATE users SET num_likes='$total_user_likes' WHERE username='$user_liked'");
$insert_user = mysqli_query($con, "DELETE FROM likes WHERE username='$userLoggedIn' AND post_id='$post_id'");
}
//Check for previous likes
$check_query = mysqli_query($con, "SELECT * FROM likes WHERE username='$userLoggedIn' AND post_id='$post_id'");
$num_rows = mysqli_num_rows($check_query);
if($num_rows > 0) {
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 {
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>
';
}
?>
</body>
</html>