-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
294 lines (253 loc) · 10.6 KB
/
search.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
<?php
//thinking of cross user searching and adding. originally just search your own database/implement mal api
// database data redundancy in database
// duplicacy of data management from adding and removing titles
session_start();
// Check if the user is already logged in, if yes then redirect him to welcome page
//<- login if start ->
if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true) {
// Include config file
$title = "Search AniDEX";
$act2 = "active";
$act1 = $act3 = $act4 = "";
global $added, $ref;
include "./includes/header.php";
// include the config file that we created before
require_once "./admin/config.php";
//search database
//<- submit if search start ->
// if (isset($_POST['submit'])) {
// this is called a try/catch statement
try {
// FIRST: Connect to the database
$connection = new PDO($dsn, $username, $password, $options);
$userid = $_SESSION['id'];
// SECOND: Create the SQL
//working ish
// $sql = "SELECT * FROM anibase WHERE anibase.animename like :search and userid= :id
// union SELECT * from anibase where anibase.animename like :search group by animename order by animename";
$sql = "SELECT Distinct a.animeid, a.userid,a.animename, a.episodes, a.image, a.imageup
FROM anibase as a
LEFT JOIN users
ON a.animename like :search and userid=:id left join anibase as b on a.animename like :search group by animename";
// THIRD: Prepare the SQL
$statement = $connection->prepare($sql);
$statement->bindValue(':id', $userid);
$statement->bindValue(':search', "%%");
$statement->execute();
// FOURTH: Put it into a $result object that we can access in the page
$result = $statement->fetchAll();
//<- add/removeing start ->
if (isset($_GET['addid']) && !empty($_GET['addid'])) {
try {
//<-select anime row from other user start ->
$connection = new PDO($dsn, $username, $password, $options);
$id = $_GET['addid'];
// echo $id;
$sql3 = "SELECT * FROM anibase where animeid = :id";
$statement3 = $connection->prepare($sql3);
$statement3->bindValue(':id', $id);
$statement3->execute();
$result3 = $statement3->fetchAll();
//<-select anime row from other user end ->
if ($statement3->rowCount() > 0) {
foreach ($result3 as $row3) {
//<-add anime name from other user start ->
$animename = $row3['animename'];
if(!empty($row3['imageup'])){
$imageup = $row3['imageup'];
$image = "0";
}
if(!empty($row3['image'])){
$image = $row3['image'];
$imageup = "0";
}
$episodes = "0";
$userid = $_SESSION['id'];
$sql4 = "INSERT INTO anibase (userid, animename, episodes, image, imageup) VALUES (:userid, :animename, :episodes, :image, :imageup)";
$statement4 = $connection->prepare($sql4);
$statement4->bindValue(":userid", $userid);
$statement4->bindValue(":animename", $animename);
$statement4->bindValue(":episodes", $episodes);
$statement4->bindValue(":image", $image);
$statement4->bindValue(":imageup", $imageup);
$statement4->execute();
$added = "<div class='container-fluid bg-success'><h3>$animename Successfully Added to List.</p></div>";
$ref = "<div class='container-fluid bg-success'><p>Results Refresh...</p></div>";
echo $added;
echo $ref;
?>
<script> setTimeout(function() { window.location = "search.php"; }, 1000);</script>
<?php
//<-add anime name from other user end ->
}
}
}
catch(PDOException $error) {
echo $sql4 . "<br>" . $error->getMessage();
}
} else {
// echo "Unknow ID";
}
if (isset($_GET['removeid']) && !empty($_GET['removeid'])) {
$connection = new PDO($dsn, $username, $password, $options);
$id = $_GET["removeid"];
$sql7 = "SELECT * FROM anibase where animeid = :id";
$statement7 = $connection->prepare($sql7);
$statement7->bindValue(':id', $id);
$statement7->execute();
$result7 = $statement7->fetchAll();
try {
// define database connection
$connection = new PDO($dsn, $username, $password, $options);
// set id variable
foreach ($result7 as $row7) {
$animename = $row7['animename'];
}
// Create the SQL
$sql2 = "DELETE FROM anibase WHERE animeid = :id";
// Prepare the SQL
$statement2 = $connection->prepare($sql2);
// bind the id to the PDO
$statement2->bindValue(':id', $id);
// execute the statement
$statement2->execute();
// $result2 = $statement2->fetchAll();
//error control of invalid variable
if (empty($animename)){
$animename = "";
}
echo "<div class='container-fluid bg-success'><h3>$animename Successfully Removed from List.</p></div>";
echo "<div class='container-fluid bg-success'><p>Results Refreshing...</p></div>";
?>
<script> setTimeout(function() { window.location = "search.php"; }, 1000);</script>
<?php
}
catch(PDOException $error) {
// if there is an error, tell us what it is
echo $sql2 . "<br>" . $error->getMessage();
}
}
//<- add/removeing start ->
?>
<div class="container-fluid">
<h2>AniDEX Anime Database</h2>
<div class="search_query" method="post">
<input type="text" id="search_query" placeholder="Search for an Anime.." name="search">
<button type="submit" name="submit">
<i class="fa fa-search">
</i>
</button>
</div>
<table class="table">
<thead>
<tr>
<th>Anime</th>
<th>Episodes Watched</th>
<th>Add/Remove</th>
</tr>
</thead>
<tbody class="search_table">
<?php foreach ($result as $row){ ?>
<tr>
<td>
<div class="row">
<div class="col-sm-6 center-block col-md-6 col-lg-6 col-sm-push-3">
<?php
echo "<b>". $row['animename'] . "</b>";
?>
</div>
</div>
<div class="col-sm-6 col-sm-offset-6 col-md-offset-7 col-lg-offset-8 col-md-4 col-lg-2 col-sm-pull-3">
<?php
switch(TRUE)
{
case (!empty($row['image']) && $row['imageup'] == 0):
echo "<img class='img-fluid img-thumbnail thumbnail' src='" . $row["image"] . "' alt='" . $row['animename'] . "'>";
break;
case (!empty($row["imageup"]) && $row['image'] == 0):
echo "<img class='img-fluid img-thumbnail thumbnail' src='" . $row["imageup"] . "' alt='" . $row['animename'] . "'>";
break;
case (empty($row['image']) || $row['image'] == 0):
echo "<p class='small'>No image available.</p>";
break;
case (empty($row['imageup']) || $row['imageup'] == 0):
echo "<p class='small'>No image available.</p>";
break;
default: echo "<p class='small'>No image available.</p>";
}
?>
</div>
</div>
</td>
<td>
<?php
// echo $row2;
if ($row['episodes'] && $userid == $row['userid']){
echo $row['episodes'];
}
else{
echo "0";
}
// break;
//}
// }
?>
</td>
<td>
<?php if ($statement->rowCount() > 0 && $userid == $row['userid']) {?>
<a href="search.php?removeid=<?php echo $row['animeid'];?>" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-remove"></span></a>
<?php } else {?>
<a href="search.php?addid=<?php echo $row['animeid'];?>" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-plus"></span></a>
<?php } ?>
</td>
</tr>
<?php
// }
// break;
}
// break;
// }
?>
</tbody>
</table>
<?php
///<-check if anime is in database end->
//needed more time to implement add feature if not found
//true to see feature
if (false) {
?>
<div class="container-fluid sResult">
<h2>Results for: "<span class="getid"></span>"</h2>
<div class="results">
<form class="addform" method="get">
<span class="container">"<span class="getid"></span>" does not appear to have been added to the database would you like to add "<span class="getid"></span>"
<a href="add.php?name=<?php echo $getid?>" class="btn btn-default btn-sm">
<span type="submit" name="submit" class="glyphicon glyphicon-plus"></span>
</a>
</span>
</form>
</div>
</div>
</div>
<?php
// var_dump($result);
}
}
catch(PDOException $error) {
// if there is an error, tell us what it is
echo $sql . "<br>" . $error->getMessage();
}
?>
</div>
</body>
</html>
<?php
//} //<- submit search end ->
}
//<- login if end ->
else {
header("location: login.php");
exit;
}
?>