-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlikes.php
More file actions
118 lines (81 loc) · 2.71 KB
/
likes.php
File metadata and controls
118 lines (81 loc) · 2.71 KB
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
<html>
<head>
<title>
<link rel = "stylesheet" type = "text/css" href = "css/style.css">
<link href="https://fonts.googleapis.com/css?family=Noto+Sans|Open+Sans:300|Slabo+27px|Source+Sans+Pro:200,400" rel="stylesheet">
</title>
</head>
<body>
<?php
require 'connection.php';
include("user.php");
include("posts.php");
if(isset($_SESSION['username']))
{
$user_logged = $_SESSION['username'];
$user_details = mysqli_query($connect, "SELECT * FROM users WHERE username = '$user_logged'");
$user = mysqli_fetch_array($user_details);
}
else{
header("Location: register.php");
}
if(isset($_GET['post_id'])) //getting post id
{
$post_id = $_GET['post_id'];
}
$get_likes = mysqli_query($connect, "SELECT likes ,post_by FROM posts WHERE id = '$post_id'");
$row = mysqli_fetch_array($get_likes);
$total_likes = $row['likes'];
$post_by = $row['post_by'];
//
if(isset($_POST['like_button']))
{
$total_likes++;
$query = mysqli_query($connect , "UPDATE posts SET likes='$total_likes' WHERE id = '$post_id'");
$likes_query = mysqli_query($connect , "INSERT INTO post_likes VALUES('', '$user_logged' , '$post_id')") ;
}
///
if(isset($_POST['unlike_button']))
{
$total_likes--;
$query = mysqli_query($connect , "UPDATE posts SET likes='$total_likes' WHERE id = '$post_id'");
$likes_query = mysqli_query($connect , "DELETE FROM post_likes WHERE liked_by = '$user_logged' AND post_id = '$post_id'");
}
$check_query = mysqli_query($connect, "SELECT id FROM post_likes WHERE liked_by='$user_logged' AND post_id='$post_id'") or die('error hai!');
$num_rows = mysqli_num_rows($check_query);
if($num_rows > 0) {
echo '<form action ="likes.php?post_id=' . $post_id . '" method = "POST" id ="like_heart">
<input type= "submit" name ="unlike_button" value ="" style="background:url(images/icons/liked.png) no-repeat ;border: none; cursor:pointer;
height: 45px;
width: 50px;
margin: -10px;">
<div style =" color: #42c3ac;
font-family: arial;
margin-left: 45px;
position: absolute;
margin-top: -18px;">
'. $total_likes .' Likes
</div>
</form>
';
}
else {
echo '<form action ="likes.php?post_id=' . $post_id . '" method = "POST" id = "unlike_heart">
<input type= "submit" name ="like_button" value ="" style="background:url(images/icons/unliked.png) no-repeat; border: none;cursor:pointer;
height: 45px;
width: 50px;
margin: -10px;">
<div style =" color: #42c3ac;
font-family: arial;
margin-left: 45px;
position: absolute;
margin-top: -18px;">
'. $total_likes .' Likes
</div>
</form>
';
}
//<input type = "submit" class = "comment_unlike" name ="unlike_button" value ="Unlike">
//<input type = "submit" class = "comment_like" name ="like_button" value ="like">
?>
</body>