-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlike.php
96 lines (78 loc) · 3.73 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
<!-- Like.php^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<link rel="stylesheet" type="text/css" href="assets/style.css">
<link rel="stylesheet" href="assets/fontawesome-free-5.15.1-web/css/all.css">
<style type="text/css">
body{
background: #fff;
}
</style>
</head>
<body>
<?php
include 'session-file.php';
include 'database/classes/User.php';
include 'database/classes/Post.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");
}
if (isset($_GET['post_id'])){
$post_id = $_GET['post_id'];
}
$get_like = mysqli_query($con, "select likes, added_by from posts where id='$post_id'");
$row = mysqli_fetch_array($get_like);
$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_btn'])){
$total_likes++;
$query = mysqli_query($con, "update posts set likes='$total_likes' where id='$post_id'")or die(mysqli_error($con));
$total_user_likes++;
$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 values('','$userLoggedIn','$post_id')");
}
//unlike button
if(isset($_POST['unlike_btn'])){
$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_query = mysqli_query($con, "delete from likes where username='$userLoggedIn' and post_id='$post_id'");
}
//chech previus likes
$check_query = mysqli_query($con, "select * from likes where username='$userLoggedIn' AND post_id='$post_id'")or die(": ( ".mysqli_error($con));
$num_rows = mysqli_num_rows($check_query);
if($num_rows > 0){ //unlike button
echo '<form action="like.php?post_id='. $post_id . '" method="POST" style="position: absolute; top: 0;">
<input type="submit" class="comment_like" name="unlike_btn" value="Unlike" style="background: #3875C5; border: none; border-radius: 3px; padding: 3px 10px 3px 10px; color: white;">
<div class="like_value" style="display: inline;">
('. $total_likes . ')
</div>
</form>
';
}
else { //like button
echo '<form action="like.php?post_id='. $post_id . '" method="POST" style="position: absolute; top: 0;">
<input type="submit" class="comment_like" name="like_btn" value="like" style="background: #3875C5; border: none; border-radius: 3px; padding: 3px 10px 3px 10px; color: white;">
<div class="like_value" style="display: inline;">
('. $total_likes . ')
</div>
</form>
';
}
?>
</body>
</html>