-
Notifications
You must be signed in to change notification settings - Fork 0
/
detail.php
365 lines (316 loc) · 18.7 KB
/
detail.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
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>
Smoke Games - Game Info
</title>
<?php include "references.php"; ?>
</head>
<body>
<?php include "navigation.php";
try{
include "config.php";
// Retrieving details of the game requested
$retrieve = $gamesdb->prepare("SELECT * FROM Games WHERE GameID = ?");
$retrieve->execute([$_GET['id']]);
// If one result is returned from query, collect information on game, to display in page
if ($retrieve->rowCount() == 1){
$row = $retrieve->fetch(PDO::FETCH_ASSOC);
$id = $row['GameID'];
$name = $row['Gname'];
$desc = $row['Description'];
$img = $row['Gimg1'];
$squareImg = $row['GimgSquare'];
$age = $row['AgeRating'];
$category = $row['Category'];
$credits = $row['Credits'];
$howto = $row['HowTo'];
$avgrate = round($row['AvgRating'], 1);
$loggedIn = false;
$oldEnough = true;
if (isset($_SESSION['id'])) {
// Retrieving age of user
$ageRetrieve = $gamesdb->prepare("SELECT Age FROM Users WHERE UID = ?");
$ageRetrieve->execute([$_SESSION['id']]);
if ($ageRetrieve->rowCount() == 1){
$row = $ageRetrieve->fetch(PDO::FETCH_ASSOC);
$userAge = $row['Age'];
$loggedIn = true;
if ($userAge < $age) {
$oldEnough = false;
}
}
}
// else if id in url not linked to a game, send user to error page
} else {
echo "<script type='text/javascript'>location.href = '404.php';</script>";
}
// Checking details entered in review field alright for database
function verifyReview() {
$review = $_POST['review'];
$errors = array();
// Checking review doesn't include any tags
if ($review != strip_tags($review)) {
array_push($errors, "Please don't use tags in your review\n");
}
return $errors;
}
// Sending a new review to the database
if($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST['leaveReview'])) {
$rErrors = verifyReview();
if (empty($rErrors)) {
// Collect details to send to database
$reviewer = $_SESSION['id'];
$rating = $_POST['rating'];
$review = $_POST['review'];
$date = date('Y-m-d');
$gameid = $_GET['id'];
$retrieve = $gamesdb->prepare("SELECT * FROM Reviews WHERE GameID = ? AND UID = ?");
$retrieve->execute([$gameid, $reviewer]);
if ($retrieve->rowCount() == 1) {
// Inserting new information into the database
$addNew = $gamesdb->prepare("UPDATE Reviews SET DateOf = ?, Rating = ?, Review = ? WHERE GameID = ? AND UID = ?");
$addNew->execute([$date, $rating, $review, $gameid, $reviewer]);
} else {
// Inserting new information into the database
$addNew = $gamesdb->prepare("INSERT INTO Reviews(GameID, DateOf, UID, Rating, Review) VALUES (?, ?, ?, ?, ?)");
$addNew->execute([$gameid, $date, $reviewer, $rating, $review]);
}
// Calculate the average rating for this game, round to 1 decimal place
$avgRating = $gamesdb->prepare("SELECT ROUND(AVG(Rating), 1) AS avgRating FROM Reviews WHERE GameID = ?");
$avgRating->execute([$gameid]);
$avgRow = $avgRating->fetch(PDO::FETCH_ASSOC);
$newAvg = $avgRow['avgRating'];
// Inserting new information into the database
$update = $gamesdb->prepare("UPDATE Games SET AvgRating = ? WHERE GameID = ?");
$update->execute([$newAvg, $gameid]);
echo "<script type='text/javascript'>alert('Reviewed Successfully.')</script>";
} else {
echo "<script type='text/javascript'>alert(". json_encode($rErrors) .");</script>";
}
}
}
} catch(PDOException $e) {
echo "<script type='text/javascript'>location.href = '404.php'";
}
$gamesdb = null;
?>
<div id="all">
<div id="content">
<div class="container">
<!-- Links back to home page -->
<div class="col-md-12">
<ul class="breadcrumb">
<li><a href="index.php">Home</a></li>
<li><a href="games.php">Games</a></li>
<li><?php echo $name; ?></li>
</ul>
</div>
<div class="col-md-3">
<div class="panel panel-default sidebar-menu">
<!-- Display high scores for this game -->
<div class="panel-heading">
<h3 class="panel-title">Leaderboards</h3>
</div>
<div class="panel-body">
<ul class="nav nav-pills nav-stacked category-menu">
<li>
<?php
try {
include "config.php";
// Retrieve top 10 scores from the Scores table
$retrieve = $gamesdb->prepare("SELECT UID, Score FROM Scores WHERE GameID = ? ORDER BY Score DESC LIMIT 10");
$retrieve->execute([$_GET['id']]);
if ($retrieve->rowCount() > 0) {
echo "<a href='#'>High Scores: <span class='badge pull-right'></span></a><ul>";
foreach ($retrieve as $row) {
// For each score retrieved, display the name of the user, and their score
$uid= $row['UID'];
$score = $row['Score'];
$findpro = $gamesdb->prepare("SELECT ProName FROM Profiles WHERE UID = ?");
$findpro->execute([$uid]);
$prow = $findpro->fetch(PDO::FETCH_ASSOC);
$pname = $prow['ProName'];
echo "<li style='margin-left:10%; margin-bottom:3%;'>$pname: $score</li>";
}
echo "</ul>";
} else {
// If no scores are found, display message informing user of this instead of leaving blank space
echo "<a href='#'>--No Scores Found--</a>";
}
} catch(PDOException $e) {
echo "<script type='text/javascript'>location.href = '404.php'";
}
$gamesdb = null;
?>
</li>
<li>
<?php
// If an average rating exists...
if ($avgrate != 0) {
// Display average rating
echo "<a href='#'>Average rating: <span class='badge pull-right'>$avgrate</span></a>";
// If they're aren't any reviews for this game, tell the user that neatly
} else {
echo "<a href='#'>--No Rating Yet--</a>";
}
?>
</li>
</ul>
</div>
</div>
</div>
<div class="col-md-9">
<div class="row" id="productMain">
<div class="col-sm-6">
<div id="mainImage">
<img src="<?php echo $img; ?>" alt="" class="img-responsive">
</div>
</div>
<div class="col-sm-6">
<div class="box">
<h1 class="text-center"><?php echo $name; ?></h1>
<?php if ($credits != "") {echo "<p style='text-align: center'>By: $credits</p>";}?>
<p style='text-align: center'>Age: <?php echo $age; ?>+</p>
<p class="text-center buttons">
<?php
if (!$loggedIn){
echo "-- You must be logged in to play this game --";
} else {
if (!$oldEnough) {
echo "-- Sorry, you aren't old enough to play this game --";
} else {
if (strpos($category, 'Multiplayer') !== false) {
echo "<a href='play.php?id=".$_GET['id']."' class='btn btn-primary' style='font-size: 16pt'><i class='fa fa-play'></i> PLAY GAME</a>";
} else {
echo "<a href='playSP.php?id=".$_GET['id']."' class='btn btn-primary' style='font-size: 16pt'><i class='fa fa-play'></i> PLAY GAME</a>";
}
}
}
?>
</p>
</div>
<div class="row" id="thumbs">
<div class="col-xs-4">
<a href="<?php echo $img; ?>" class="thumb">
<img src="<?php echo $squareImg; ?>" alt="" class="img-responsive">
</a>
</div>
<div class="col-xs-4">
<a href="<?php echo $img; ?>" class="thumb">
<img src="<?php echo $squareImg; ?>" alt="" class="img-responsive">
</a>
</div>
<div class="col-xs-4">
<a href="<?php echo $img; ?>" class="thumb">
<img src="<?php echo $squareImg; ?>" alt="" class="img-responsive">
</a>
</div>
</div>
</div>
</div>
<div class="box" id="details">
<p>
<h3>Game details</h3>
<p><?php echo $desc; ?></p>
<?php if ($howto != "") {echo "<h4>How to play:</h4> <p>$howto</p>";}?>
</p>
</div>
<div class="box" id="reviews">
<?php
try{
include "config.php";
// Retrieve all ratings and reviews for this game
$retrieve = $gamesdb->prepare("SELECT r.Rating, r.Review, r.DateOf, p.ProName FROM Reviews r JOIN Profiles p ON r.UID = p.UID WHERE r.GameID = ?");
$retrieve->execute([$_GET['id']]);
$count = $retrieve->rowCount();
if ($count > 0) {
// Display number of reviews
echo "<h3>$count Reviews</h3>";
// For each review display the profile name of the user, their rating, and review, and the date the review was made
foreach ($retrieve as $row) {
$rating = $row['Rating'];
$review = $row['Review'];
$daterev = $row['DateOf'];
$rname = $row['ProName'];
echo "<p><h5><span style='font-size:16pt'>$rname</span> : $rating / 5</h5><h5>$daterev</h5>$review</p>\r\n";
}
} else {
// If they're aren't any reviews for this game, tell the user that neatly
echo "<a href='#'>--No Reviews Yet--</a>";
}
}catch(PDOException $e) {
echo "<script type='text/javascript'>location.href = '404.php'";
}
$gamesdb = null;
?>
<hr>
<?php
if (isset($_SESSION['username'])) {
//Form for users to leave reviews of the game
echo "<div id='comment-form' data-animate='fadeInUp'>";
echo "<h4>Leave a review</h4>";
echo "<form action='detail.php?id=$id' method='post'>";
echo "<div class='row'>";
echo "<div class='col-sm-12'>";
echo "<div class='form-group'>";
echo "<label for='rating'>Rating out of 5 <span class='required'>*</span></label>";
echo "<input class='form-control' type='number' id='rating' name='rating' required='required' min='1' max='5'>";
echo "<label for='review'>Review <span class='required'>*</span></label>";
echo "<textarea class='form-control' id='review' name='review' rows='4' required='required'></textarea>";
echo "</div></div></div>";
echo "<div class='row'>";
echo "<div class='col-sm-12 text-right'>";
echo "<button class='btn btn-primary' type='submit' name='leaveReview'><i class='fa fa-comment-o'></i> Post review</button>";
echo "</div></div></form></div>";
} else {
echo "<p>--You must be logged in to leave a review--</p>";
}
?>
</div>
<div class="row" id="productMain">
<div class="col-xs-3">
<div class="box same-height">
<h2>Suggested Games: </h2>
</div>
</div>
<?php
try{
include "config.php";
$retrieve = $gamesdb->prepare("SELECT * FROM Games WHERE Recommended = 1 ORDER BY Gname ASC LIMIT 3");
$retrieve->execute();
if($retrieve->rowCount() > 0) {
foreach($retrieve as $row){
$recid = $row["GameID"];
$recname = $row["Gname"];
$recimg = $row["Gimg1"];
echo "<div class='row same-height-row'>";
echo "<div class='col-xs-3'><div class='product same-height'>";
echo "<div class='flip-container'><div class='flipper'><div class='front'>";
echo "<a href='detail.php?id=$recid'><img src='$recimg' alt='' class='img-responsive'></a>";
echo "</div><div class='back'><a href='detail.php?id=$recid'>";
echo "<img src='$recimg' alt='' class='img-responsive'></a></div></div></div>";
echo "<a href='detail.php?id=$recid' class='invisible'><img src='$recimg' alt='' class='img-responsive'>";
echo "</a><div class='text'><h3>$recname</h3></div></div></div>";
}
echo "</div>";
} else {
echo "<p>--No results</p>";
}
}catch(PDOException $e) {
echo "<script type='text/javascript'>location.href = '404.php'";
}
$gamesdb = null;
?>
</div>
</div>
</div>
</div>
</div>
<?php include "footer.php"; ?>
</body>
</html>