-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
239 lines (190 loc) · 6.31 KB
/
index.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
session_start();
require_once("pdo.php");
if (isset($_POST["votes"])) {
//only upvote if logged in
if (isset($_SESSION["account"])) {
$user=$_SESSION["account"];
$sno = $_POST["sno"];
$postid=$sno;
$sql = "Select votes from blogs.blogs where sno= :sno";
$stmt = $pdo->prepare($sql);
$stmt->execute(
array(
':sno' => $sno
)
);
$sqlnew="Insert into blogs.likes ( `user`, `postid`) VALUES (:user, :postid);";
$stmtnew = $pdo->prepare($sqlnew);
$votes = 0;
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row != false) {
$votes = $row["votes"];
$votes++;
$sql = "Update blogs.blogs SET votes= :votes where sno = :sno";
$stmt = $pdo->prepare($sql);
$stmt->execute(
array(
':votes' => $votes,
':sno' => $sno
)
);
$stmtnew->execute(
array(
':user' => $user,
':postid' => $postid
)
);
//new fetch code- the response is only number of votes
echo $votes;
exit();
}
}
}
if (isset($_POST["unvotes"])) {
//only upvote if logged in
if (isset($_SESSION["account"])) {
$user=$_SESSION["account"];
$sno = $_POST["sno"];
$postid=$sno;
$sql = "Select votes from blogs.blogs where sno= :sno";
$stmt = $pdo->prepare($sql);
$stmt->execute(
array(
':sno' => $sno
)
);
$sqlnew="Delete from blogs.likes where `user`= :user AND `postid`= :postid;";
$stmtnew = $pdo->prepare($sqlnew);
$votes = 0;
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row != false) {
$votes = $row["votes"];
$votes--;
$sql = "Update blogs.blogs SET votes= :votes where sno = :sno";
$stmt = $pdo->prepare($sql);
$stmt->execute(
array(
':votes' => $votes,
':sno' => $sno
)
);
$stmtnew->execute(
array(
':user' => $user,
':postid' => $postid
)
);
//new fetch code- the response is only number of votes
echo $votes;
exit();
}
}
}
require "header.php";
//
$sql = "Select * from blogs.blogs ORDER BY RAND() ";
$stmt = $pdo->query($sql);
echo '<div class="entry">';
echo '<h1>All blogs are displayed here</h1>';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
?>
<div class="content">
<div class="title">
<h2>
<?php echo $row["title"]; ?></h2>
</div>
<div class="name" style="color:red; font-size:large; text-align:right; text-transform:capitalize">
<?php echo $row["user"]; ?></h2>
</div>
<?php echo $row["content"]; ?></h2>
</div>
<div class="upvote">
<span id="likes_count<?php echo $row["sno"]; ?>"><?php echo $row["votes"]; ?> Votes</span>
<?php if (!isset($_SESSION["account"])) { ?>
<button type="button" class="button" name="votes" onclick="like(<?php echo $row['sno']; ?>)" id="votes"><img
src="assets/like.jpg" /></button>
<?php } else {
$user = $_SESSION["account"];
$postid=$row["sno"];
$sql2="Select * from blogs.likes WHERE `user`= :user and `postid`= :postid";
$stmt2 = $pdo->prepare($sql2);
$stmt2->execute(
array(
':user' => $user,
':postid' => $postid
)
);
//liked
if($stmt2->fetch(PDO::FETCH_ASSOC))
{ ?>
<button type="button" class="button" name="votes" onclick="unlike(<?php echo $row['sno']; ?>)"
id="unlike<?php echo $row['sno']; ?>">
<img src="assets/unlike.png" id="imgunlike<?php echo $row["sno"]; ?>" /></button>
<button type="button" class="button hide" onclick="like(<?php echo $row['sno']; ?>)"
id="like<?php echo $row['sno']; ?>"><img src="assets/like.jpg"
id="imglike<?php echo $row["sno"]; ?>" /></button>
<?php }
// not liked
else {?>
<button type="button" class="button hide" name="votes" onclick="unlike(<?php echo $row['sno']; ?>)"
id="unlike<?php echo $row['sno']; ?>">
<img src="assets/unlike.png" id="imgunlike<?php echo $row["sno"]; ?>" /></button>
<button type="button" class="button" onclick="like(<?php echo $row['sno']; ?>)"
id="like<?php echo $row['sno']; ?>"><img src="assets/like.jpg"
id="imglike<?php echo $row["sno"]; ?>" /></button>
<?php } } ?>
</div>
<?php
}
echo '</div>'; ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
function like(sn) {
var sno = sn;
var votes = true;
$.ajax({
url: 'index.php',
type: 'post',
data: {
votes: votes,
sno: sno
},
success: function(response) {
if (isNaN(response)) {
window.location.href = "login.php";
} else {
document.getElementById(`likes_count${sno}`).textContent = response + " Votes";
document.getElementById(`like${sno}`).classList.add("hide");
document.getElementById(`unlike${sno}`).classList.remove("hide");
console.log(response);
}
}
});
}
function unlike(sn) {
var sno = sn;
var unvotes = true;
$.ajax({
url: 'index.php',
type: 'post',
data: {
unvotes: unvotes,
sno: sno
},
success: function(response) {
if (isNaN(response)) {
window.location.href = "login.php";
} else {
document.getElementById(`likes_count${sno}`).textContent = response + " Votes";
document.getElementById(`unlike${sno}`).classList.add("hide");
document.getElementById(`like${sno}`).classList.remove("hide");
console.log(response);
}
}
});
}
</script>
<?php
include "footer.php";
?>