forked from chriszhou1992/ABCD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunlike.php
211 lines (157 loc) · 5.58 KB
/
unlike.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
<?php
session_start();
require_once "classes/connecToDB.php";
$con = connectToDB();
if($con === false):
return;
endif;
if(isset($_POST["game"])):
$game = addslashes($_POST["game"]);
$sql = "SELECT num_likes FROM games WHERE name = '$game'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
$row = mysqli_fetch_array($result);
$num_likes = $row['num_likes'] - 1;
$sql = "UPDATE games SET num_likes = '$num_likes' WHERE name = '$game'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
echo $num_likes;
removeFromGameLikedList($con, $_SESSION["user"], $game);
removeFromSuggestList($con, $_SESSION["user"], $game);
removeFromglikedList($con, $_SESSION["user"], $game);
endif;
if(isset($_POST["company"])):
$company = addslashes($_POST["company"]);
$sql = "SELECT num_likes FROM companies WHERE name = '$company'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
$row = mysqli_fetch_array($result);
$num_likes = $row['num_likes'] - 1;
$sql = "UPDATE companies SET num_likes = '$num_likes' WHERE name = '$company'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
echo $num_likes;
removeFromCompanyLikedList($con, $_SESSION["user"], $company);
endif;
function removeFromGameLikedList($con, $username, $game) {
$sql = "SELECT gamesLiked FROM users WHERE email = '$username'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
$row = mysqli_fetch_array($result);
$gamesLiked = explode("<br>", $row['gamesLiked']);
if (($key = array_search($game, $gamesLiked)) !== false) {
unset($gamesLiked[$key]);
}
$gamesLiked = implode("<br>", $gamesLiked);
$gamesLiked = addslashes($gamesLiked);
$sql = "UPDATE users SET gamesLiked = '$gamesLiked' WHERE email = '$username'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
}
function removeFromCompanyLikedList($con, $username, $company) {
$sql = "SELECT companiesLiked FROM users WHERE email = '$username'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
$row = mysqli_fetch_array($result);
$companiesLiked = explode("<br>", $row['companiesLiked']);
if (($key = array_search($company, $companiesLiked)) !== false) {
unset($companiesLiked[$key]);
}
$companiesLiked = implode("<br>", $companiesLiked);
$companiesLiked = addslashes($companiesLiked);
$sql = "UPDATE users SET companiesLiked = '$companiesLiked' WHERE email = '$username'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
}
function removeFromSuggestList($con, $username, $game) {
$sql = "SELECT similar_games FROM games WHERE name = '$game'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
$row = mysqli_fetch_array($result);
if(strlen($row['similar_games']) === 0) {
$similar = array();
}
else {
$similar = $row['similar_games'];
$similar = explode("<br>", $similar);
$similar = array_slice($similar, 1);
}
$sql = "SELECT gamesMightLike FROM users WHERE email = '$username'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
$row = mysqli_fetch_array($result);
if(strlen($row['gamesMightLike']) === 0) {
$mightLike = array();
} else {
$mightLike = explode("<br>", $row['gamesMightLike']);
$mightLike = array_slice($mightLike, 1);
}
$lenArr = count($similar);
for($i=0; $i<$lenArr; $i++):
if (($key = array_search($similar[$i], $mightLike)) !== false) {
unset($mightLike[$key]);
}
endfor;
if(count($mightLike) === 0) {
$newGamesMightLike = NULL;
} else {
$newGamesMightLike = "<br>".implode("<br>", $mightLike);
}
$newGamesMightLike = addslashes($newGamesMightLike);
$sql = "UPDATE users SET gamesMightLike = '$newGamesMightLike' WHERE email = '$username'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
}
function removeFromglikedList($con, $username, $game) {
$sql = "SELECT publishers FROM games WHERE name = '$game'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
$row = mysqli_fetch_array($result);
if(count($row['publishers']) > 1) {
/* to be added */
return;
} else {
$publisher = $row['publishers'];
}
$sql = "SELECT glikedCompany FROM users WHERE email = '$username'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
$row = mysqli_fetch_array($result);
$gliked = $row['glikedCompany'];
$gliked = explode("<br>", $gliked);
$key = array_search($publisher, $gliked);
unset($gliked[$key]);
$gliked = implode("<br>", $gliked);
$gliked = addslashes($gliked);
$sql = "UPDATE users SET glikedCompany = '$gliked' WHERE email = '$username'";
$result = mysqli_query($con, $sql);
if(!$result) {
die('Error: ' . mysqli_error($con));
}
}