-
Notifications
You must be signed in to change notification settings - Fork 2
/
player-search.php
281 lines (257 loc) · 9.47 KB
/
player-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
<?php
include_once "includes/header.php";
loginCheck();
$teamid = execute_query("SELECT TeamID FROM Team WHERE UserID=?", array($_SESSION['UserID']))['rows_affected'][0]['TeamID'];
// Current Team Members
$sql2 = "SELECT RLPID, TeamID FROM TeamPlayer WHERE TeamID=?";
$team_players = execute_query($sql2, array($teamid));
$current_players = array();
for ($i = 0; $i < $team_players['row_count']; $i++) {
array_push($current_players, intval($team_players['rows_affected'][$i]['RLPID']));
}
$sql = "SELECT RLPID, name, position, mvps, goals, assists FROM RLPlayer";
$search_params = array();
if (isset($_POST['POST-TYPE'])) {
if ($_POST['POST-TYPE'] == 'Add/Remove') {
// Get clicked player button
foreach ($_POST as $button => $val) {
$button_info = explode('-', $button);
if (count($button_info) == 3) {
$RLPID = intval($button_info[1]);
switch ($button_info[2]) {
case 'add':
if (count($current_players) < 5) {
$timestamp = date('Y-m-d H:i:s');
$add_sql = "INSERT INTO TeamPlayer VALUES (?, ?, ?, 0, 0, 0)";
$addinfo = execute_query($add_sql, array($RLPID, $teamid, $timestamp));
array_push($current_players, $RLPID);
}
break;
case 'remove':
if (count($current_players) > 0) {
$remove_sql = "DELETE FROM TeamPlayer WHERE TeamID=? AND RLPID=?";
// Delete player from team in DB
$removeinfo = execute_query($remove_sql, array($teamid, $RLPID));
if (($idx = array_search($RLPID, $current_players)) !== false) {
unset($current_players[$idx]);
}
}
break;
default:
echo "ERROR";
break;
}
}
}
} else if ($_POST['POST-TYPE'] == 'Search') {
$_SESSION['player-search'] = $_POST['player-search'];
redirectTo('/player-search/page/1');
}
}
// Clears search terms if redirecting from another page
if (isset($_SERVER['HTTP_REFERER'])) {
if (!strpos($_SERVER['HTTP_REFERER'], 'player-search')) {
unset($_SESSION['player-search']);
unset($_SESSION['sort']);
}
}
if (isset($_SESSION['player-search'])) {
// Update results to match search terms
$searchInput = "%" . $_SESSION['player-search'] . "%";
$sql .= ' WHERE name LIKE ?';
array_push($search_params, $searchInput);
}
if (isset($_POST['sort'])) {
$_SESSION['sort'] = $_POST['sort'];
redirectTo('/player-search/page/1');
}
if (isset($_SESSION['sort'])) {
switch ($_SESSION['sort']) {
case 'Name_A_Z':
// Sort by ascending name
$sql .= " ORDER BY name";
break;
case 'Name_Z_A':
// Sort by descending name
$sql .= " ORDER BY name DESC";
break;
case 'Goals':
// Sort by descending goals
$sql .= " ORDER BY goals DESC";
break;
case 'Assists':
// Sort by descending assists
$sql .= " ORDER BY assists DESC";
break;
case 'MVPs':
// Sort by descending mvps
$sql .= " ORDER BY mvps DESC";
break;
default:
break;
}
}
$pageno = 1;
if (isset($_GET['pageno'])) {
$pageno = intval($_GET['pageno']);
}
$no_of_records_per_page = 10;
$offset = ($pageno - 1) * $no_of_records_per_page;
$total_pages = execute_query($sql, $search_params)['row_count'] / $no_of_records_per_page;
$sql .= " LIMIT $offset, $no_of_records_per_page";
$data = execute_query($sql, $search_params);
function actionsTD($RLPID)
{
global $current_players;
$style = 'width: 80%;
color: white;
border: none;
padding: 1rem;
font-family: \'Open Sans\', sans-serif;
font-size: 1em;
font-weight: 700;
cursor: pointer;';
if (in_array($RLPID, $current_players)) {
$style .= ' background-color: red;';
$action = "Remove";
$RLPID .= '-remove';
} else {
if (count($current_players) == 5) {
$style .= ' background-color: gray;';
echo '<div class="ps-actions">
<button type="submit" style="' . $style . ' cursor: unset;" disabled>Roster Full</button>
</div>';
return;
}
$style .= ' background-color: green;';
$action = "Add";
$RLPID .= '-add';
}
echo '<div class="ps-actions">
<button name="button-' . $RLPID . '" type="submit" style="' . $style . '">' . $action . ' Player</button>
</div>';
}
function textTD($contents)
{
echo '<div class="ps_text">' . $contents . '</div>';
}
?>
<link rel="stylesheet" href=<?php echo transformPath('/css/player-search.css') ?>>
<div class="inner-page-contents">
<div class="player-search">
<form class="player-search-top-section" action="" method="post">
<div class="ps-input">
<input type="text" name="POST-TYPE" value="Search" style="display: none;">
<input type="text"
name="player-search"
id="player-search"
placeholder="Search Player Info..."
<?php if (isset($_SESSION['player-search'])) {
echo 'value="' . $_SESSION['player-search'] . '"';
if (strlen($_SESSION['player-search']) == 0) {echo ' autofocus';}
}
else {echo ' autofocus';}
?>>
<i class="fas fa-search"></i>
</div>
<div class="ps-dropdown">
<label for="sort">Sort By: </label>
<select name="sort" id="sort-input" onchange="this.form.submit()">
<option <?php if (!isset($_SESSION['sort'])) {
echo 'selected';
} ?>disabled value="">----- Select Option -----</option>
<option <?php if (isset($_SESSION['sort'])) {
if ($_SESSION['sort'] == 'Name_A_Z') {
echo 'selected';
}
} ?> value="Name_A_Z">Name (A-Z)</option>
<option <?php if (isset($_SESSION['sort'])) {
if ($_SESSION['sort'] == 'Name_Z_A') {
echo 'selected';
}
} ?> value="Name_Z_A">Name (Z-A)</option>
<option <?php if (isset($_SESSION['sort'])) {
if ($_SESSION['sort'] == 'Goals') {
echo 'selected';
}
} ?> value="Goals">Goals</option>
<option <?php if (isset($_SESSION['sort'])) {
if ($_SESSION['sort'] == 'Assists') {
echo 'selected';
}
} ?> value="Assists">Assists</option>
<option <?php if (isset($_SESSION['sort'])) {
if ($_SESSION['sort'] == 'MVPs') {
echo 'selected';
}
} ?> value="MVPs">MVPs</option>
</select>
</div>
<input type="submit" style="display: none" />
</form>
<div class="player-search-bottom-section">
<form class="table-wrapper" action="" method="post">
<input type="text" name="POST-TYPE" value="Add/Remove" style="display: none;">
<table>
<thead>
<tr class="table-header">
<th>Player Name</th>
<th>Position</th>
<th style="width: 100px;">Goals</th>
<th style="width: 100px;">Assists</th>
<th style="width: 100px;">MVPs</th>
<th style="width: 200px;">Actions</th>
</tr>
</thead>
<tbody>
<?php $i = 0;
for (; $i < $data['row_count']; $i++) :
$player = $data['rows_affected'][$i]; ?>
<tr>
<td><?php textTD($player['name']) ?></td>
<td><?php textTD(substr($player['position'], 0, strlen($player['position']) - 1)) ?></td>
<td><?php textTD($player['goals']) ?></td>
<td><?php textTD($player['assists']) ?></td>
<td><?php textTD($player['mvps']) ?></td>
<td style="vertical-align: top;"><?php actionsTD($player['RLPID']) ?></td>
</tr>
<?php endfor;
for (; $i < 5; $i++) : ?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php endfor; ?>
</tbody>
</table>
</form>
<ul class="pagination" style="display: flex; margin-top: 10px;">
<li><a href=<?php echo transformPath('/player-search/page/1') ?>><i class="fas fa-angle-double-left"></i> First</a></li>
<li class="<?php if ($pageno <= 1) {
echo 'disabled';
} ?>">
<a href=<?php if ($pageno <= 1) {
echo '#';
} else {
echo transformPath('/player-search/page/' . ($pageno - 1));
} ?>><i class="fas fa-angle-left"></i> Previous</a>
</li>
<li class="<?php if ($pageno >= $total_pages) {
echo 'disabled';
} ?>">
<a href="<?php if ($pageno >= $total_pages) {
echo '#';
} else {
echo transformPath('/player-search/page/' . ($pageno + 1));
} ?>"><i class="fas fa-angle-right"></i> Next</a>
</li>
<li><a href="<?php echo transformPath('/player-search/page/' . ceil($total_pages)); ?>"><i class="fas fa-angle-double-right"></i> Last</a></li>
</ul>
</div>
</div>
</div>
<?php include_once "includes/footer.php" ?>