-
Notifications
You must be signed in to change notification settings - Fork 0
/
upvotecount.php
409 lines (333 loc) · 11.8 KB
/
upvotecount.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?php
ini_set('display_errors','1');
//include './inc/connect.inc.php';
include './inc/header.inc.php';
include './inc/textfilter.php';
include './inc/loggedInUserDetails.php';
// echo "<h1>hello</h1>";
date_default_timezone_set("Asia/Kathmandu");
$curr_date_time = date("yy-m-d h:i:s");
// echo "$curr_date_time";
?>
<?php
$post_id = $_GET['post_id'];
$upordown = $_GET['name'];
// echo $upordown;
// echo "$post_id";
// die();
//
// echo "$id and $post_id";
$queryUpvote = 'SELECT upvote, downvote, net_upvotes, respect, upvoted_by_id, downvoted_by_id, respected_by_id FROM post_upvotes WHERE upvoted_to_id = ? && post_id = ?';
$queryUpvotestmt = $conn -> stmt_init();
if($queryUpvotestmt -> prepare($queryUpvote))
{
$queryUpvotestmt -> bind_param('ii',$user_id, $post_id);
$queryUpvotestmt -> execute();
// print_r($queryUpvotestmt);
$queryUpvotestmt -> bind_result($upvotes,$downvotes,$net_upvotes, $respects, $upvotedBylist, $downvotedBylist,$respectedBylist);
$queryUpvotestmt -> store_result();
$postCount = $queryUpvotestmt -> num_rows;
// echo "you son of a bitch i am in";
}
else {
echo $queryUpvotestmt -> error;
}
while($queryUpvotestmt -> fetch())
{
}
$respectedBylistExplode = explode(", ",$respectedBylist);
$respectedBylistCount = count($respectedBylistExplode);
$respectedBylistCount -= 1;
$upvotedBylistExplode = explode(", ", $upvotedBylist);
// print_r($upvotedBylistExplode);
$upvotedBylistCount = count($upvotedBylistExplode);
$upvotedBylistCount -= 1;
// echo "$upvotedBylistCount";
$downvotedBylistExplode = explode(", ", $downvotedBylist);
// print_r($downvotedBylistExplode);
$downvotedBylistCount = count($downvotedBylistExplode);
$downvotedBylistCount -= 1;
// echo "$downvotedBylistCount";
$queryUpvotestmt -> free_result();
// echo "$upvotedBylist";
// echo "net = $net_upvotes, total up = $upvotes and down = $downvotes";
//if post is not recored in database then we have to insert the data for first upvote and then update it in next upvotes
if($postCount == 0)
{
if($upordown == "up")
{
$newupvote = 1;
$newdownvote = 0;
$newnetupvote = 1;
$newrespect = 0;
$upvotedfirsttimeid = "$myuserid, ";
$downvotedfirsttimeid = "";
$respectedfirsttimeid = "";
echo "$newupvote";
}
else if($upordown == "down")
{
$newupvote = 0;
$newdownvote = 1;
$newnetupvote = 0;
$newrespect = 0;
$downvotedfirsttimeid = "$myuserid, ";
$upvotedfirsttimeid = "";
$respectedfirsttimeid = "";
// echo "$newupvote";
}
elseif ($upordown == "respect")
{
// code...
$newrespect = 1;
$newupvote = 0;
$newdownvote = 0;
$newnetupvote = 0;
$respectedfirsttimeid = "$myuserid, ";
$downvotedfirsttimeid = "";
$upvotedfirsttimeid = "";
echo "$newrespect";
}
else {
die();
}
// IF ONE POST IS FOUND then
$upvoteInsertquery = 'INSERT INTO post_upvotes(post_id, upvote, downvote, net_upvotes,
respect, upvoted_by_id, downvoted_by_id,respected_by_id, upvoted_to_id, date_time)
VALUES(?,?,?,?,?,?,?,?,?,?)';
$upvoteInsertstmt = $conn -> stmt_init();
if($upvoteInsertstmt -> prepare($upvoteInsertquery))
{
$upvoteInsertstmt -> bind_param('iiiiisssis',$post_id, $newupvote, $newdownvote,
$newnetupvote,$newrespect, $upvotedfirsttimeid, $downvotedfirsttimeid,
$respectedfirsttimeid ,$user_id,$curr_date_time);
$upvoteInsertstmt -> execute();
// print_r($upvoteInsertstmt);
// $upvoteInsertstmt -> bind_result($net_upvotes, $upvotedBy);
// $upvoteInsertstmt -> store_result();
// $postCount = $upvoteInsertstmt -> num_rows;
// echo "you son of a bitch i am in";
$upvoteInsertstmt-> free_result();
}
else {
echo $upvoteInsertstmt -> error;
}
}
//IF 1 POST IS FOUND THEN
elseif($postCount == 1)
{
if($upordown == "up")
{
//IF I HAVE NOT UPVOTED PREVIOUSLY
if(!(in_array("$myuserid",$upvotedBylistExplode)) && !(in_array("$myuserid",$downvotedBylistExplode)))
{
$upvotes += 1;
$upvotedlist = "$myuserid, ";
$downvotedlist = "";
echo "$upvotes";
// if(strstr($downvotedBylist, "$myuserid, "))
// {
// $newdownvotedlist = str_replace("$myuserid, ","",$downvotedBylist);
// // $upvotes -= 1;
// // $upvotes += 1;
// $downvotes -= 1;
// }
}
elseif (in_array("$myuserid",$upvotedBylistExplode) || in_array("$myuserid",$downvotedBylistExplode))
{
if (in_array("$myuserid",$upvotedBylistExplode))
{
die();
// code...
}
elseif (in_array("$myuserid",$downvotedBylistExplode))
{
// code...
$upvotes += 1;
$downvotes -= 1;
$upvotedlist = "$myuserid, ";
$newdownvotedlist = str_replace("$myuserid, ","",$downvotedBylist);
echo "$upvotes";
}
// code...
}
else {
die();
}
// echo $upvote;
}
elseif ($upordown == "down")
{
//IF I AM SEEING THAT POST FOR THE FIRST TIME I.E MY FIRST UPVOTE OR DOWNVOTE TO THAT POST
if(!in_array("$myuserid",$upvotedBylistExplode) && !in_array("$myuserid",$downvotedBylistExplode))
{
$downvotes += 1;
$downvotedlist = "$myuserid, ";
$upvotedlist = "";
}
//IF I HAD ALREADY UPVOTED OR DOWNVOTED THAT POST PREVIOUSLY
elseif(in_array("$myuserid",$upvotedBylistExplode) || in_array("$myuserid",$downvotedBylistExplode))
{
//IF I HAD PREVIOUSLY UPVOTED THAT POST AND NOW I AM DODWNVOTING THEN..
if(in_array("$myuserid",$upvotedBylistExplode))
{
$downvotes += 1;
$upvotes -= 1;
$newupvotedlist = str_replace("$myuserid, ","",$upvotedBylist);
$downvotedlist = "$myuserid, ";
echo "$upvotes";
}
//IF I HAD ALREADY DOWNVOTED PREVIOUSLY AND AGAIN I CLICKED DOWNVOTE THEN DO NOTHING AND DIE
elseif (in_array("$myuserid",$downvotedBylistExplode)) {
// code...
die();
}
// $upvotes -= 1;
}
}
#################################### IF NOT BOTH UPVOTE OR DOWNVOTE IS CLICKED THEN DIE HERE COMMENT AND OTHHER BUTTONS CODE WILL GO LATER##################3
elseif ($upordown == "respect")
{
// code...
//THIS CODE HERE MEANS I AM ALREADY INTRODUCED TO THAT POST BY UPVOTING OR DODWNVOTING
//IF I CLICKED RESPECT THEN
//****FIRST CHECK IF I HAD PREVIOUSLY DID RESPECT OR NOT
//IF MY NAME IS NOT IN THE RESPECTED ID LIST THEN ONLY
if(!in_array($myuserid,$respectedBylistExplode))
{
$respects += 1;
$respectedBylist = "$myuserid, ";//concatinate this.
echo "$respects";
$respectupdatequery = 'UPDATE `post_upvotes` SET `respect`= ?, `respected_by_id`= CONCAT(respected_by_id,?) WHERE `post_upvotes`.`post_id` = ?';
$respectupdatestmt = $conn -> stmt_init();
// $upvoteupdatestmt = $conn -> stmt_init();
if($respectupdatestmt -> prepare($respectupdatequery))
{
$respectupdatestmt -> bind_param('isi', $respects, $respectedBylist, $post_id);
$respectupdatestmt -> execute();
}
else {
echo $respectupdatestmt -> error;
}
}
//IF I ALREADY HAD RESPECTED THEN END THIS
else
{
die();
}
}
//IF BUTTON PRESSED IS EXCEPT THEN UP DOWN OR RESPECT THEN END
else {
die();
}
// echo "$upvotedBylist";
// $upvotes -= 1;
// code...
}
############################## FOR CLICKING DOWNVOTE BUTTON #################################
if(isset($newupvotedlist))//i.e I want to downvote and my name_id is in upvotedlist
{
// echo "hello";
//TO NOT MAKE RESPECT BUTTON COUNT TO -1.IF BECOMES -1 THEN CHANGE IT TO ZERO
$net_upvotes = ($upvotes - $downvotes) > 0 ? ($upvotes - $downvotes) : 0;
if($net_upvotes >= 0)
{
if(($net_upvotes + 1) % 20 == 0)
{
$respect = $respects - 1;
}
else {
$respect = $respects;
}
}
// $respect = ($net_upvotes / 20) ;
$upvoteupdatequery = 'UPDATE `post_upvotes` SET `upvote` = ?, `downvote` = ?, `net_upvotes` = ?, `respect`= ?, `upvoted_by_id`= ?, `downvoted_by_id`= CONCAT(downvoted_by_id,?) WHERE `post_upvotes`.`post_id` = ?';
$upvoteupdatestmt = $conn -> stmt_init();
// $upvoteupdatestmt = $conn -> stmt_init();
if($upvoteupdatestmt -> prepare($upvoteupdatequery))
{
$upvoteupdatestmt -> bind_param('iiiissi',$upvotes, $downvotes, $net_upvotes, $respect, $newupvotedlist, $downvotedlist, $post_id);
$upvoteupdatestmt -> execute();
}
else {
echo $upvoteupdatestmt -> error;
}
}
elseif(!isset($newupvotedlist) && $upordown == "down") {//IF I CLICKED DOWNVOTE AND MY NAME IS NOT IN UPVOTEDLIST
if($net_upvotes %20 == 0 && $net_upvotes > 0)
{
$net_upvotes1 = $upvotes - $downvotes;
if($net_upvotes1 == ($net_upvotes - 1))
{
$respect = $respects - 1;
}
}
else {
$respect = $respects;
}
$net_upvotes = $upvotes - $downvotes;
// $respect = $net_upvotes / 20;
$upvoteupdatequery = 'UPDATE `post_upvotes` SET `upvote` = ?, `downvote` = ?, `net_upvotes` = ?, `respect` = ?, `upvoted_by_id`= CONCAT(upvoted_by_id,?), `downvoted_by_id`= CONCAT(downvoted_by_id,?) WHERE `post_upvotes`.`post_id` = ?';
$upvoteupdatestmt = $conn -> stmt_init();
// $upvoteupdatestmt = $conn -> stmt_init();
if($upvoteupdatestmt -> prepare($upvoteupdatequery))
{
$upvoteupdatestmt -> bind_param('iiiissi',$upvotes, $downvotes, $net_upvotes, $respect, $upvotedlist, $downvotedlist, $post_id);
$upvoteupdatestmt -> execute();
}
else {
echo $upvoteupdatestmt -> error;
}
}
############################## END FOR CLICKING DOWNVOTE BUTTON #################################
################################ FOR CLICKING UPVOTE BUTTON #######################################
if(isset($newdownvotedlist))//i.e I want to upvote and my name_id is already in downvotedlist
{
$net_upvotes = $upvotes - $downvotes;
if($net_upvotes >= 0)
{
if((($net_upvotes+ 1) % 20) == 0)
{
$respect = $respects + 1;
}
else {
// die("$respects");
$respect = $respects;
}
}
// $respect = $net_upvotes / 20;
$upvoteupdatequery = 'UPDATE `post_upvotes` SET `upvote` = ?, `downvote` = ?, `net_upvotes` = ?,`respect` = ?, `upvoted_by_id`= CONCAT(upvoted_by_id,?), `downvoted_by_id`= ? WHERE `post_upvotes`.`post_id` = ?';
$upvoteupdatestmt = $conn -> stmt_init();
// $upvoteupdatestmt = $conn -> stmt_init();
if($upvoteupdatestmt -> prepare($upvoteupdatequery))
{
$upvoteupdatestmt -> bind_param('iiiissi',$upvotes, $downvotes, $net_upvotes, $respect, $upvotedlist, $newdownvotedlist, $post_id);
$upvoteupdatestmt -> execute();
}
else {
echo $upvoteupdatestmt -> error;
}
}
elseif(!isset($newdownvotedlist) && $upordown === "up") {//IF I CLICKED UPVOTE AND MY NAME IS NOT IN DOWNVOTEDLIST
$net_upvotes = $upvotes - $downvotes;
if($net_upvotes % 20 == 0 && $respects > 0)
{
$respect = $respects + 1;
}
else {
$respect = $respects;
}
$upvoteupdatequery = 'UPDATE `post_upvotes` SET `upvote` = ?, `downvote` = ?, `net_upvotes` = ?, `respect` = ?, `upvoted_by_id`= CONCAT(upvoted_by_id,?), `downvoted_by_id`= CONCAT(downvoted_by_id,?) WHERE `post_upvotes`.`post_id` = ?';
$upvoteupdatestmt = $conn -> stmt_init();
// $upvoteupdatestmt = $conn -> stmt_init();
if($upvoteupdatestmt -> prepare($upvoteupdatequery))
{
$upvoteupdatestmt -> bind_param('iiiissi',$upvotes, $downvotes, $net_upvotes, $respect, $upvotedlist, $downvotedlist, $post_id);
$upvoteupdatestmt -> execute();
}
else {
echo $upvoteupdatestmt -> error;
}
}
################################ END FOR CLICKING UPVOTE BUTTON #######################################
?>