-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_match copy 4.php
executable file
·297 lines (263 loc) · 10.4 KB
/
insert_match copy 4.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
<?php
ob_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once 'conn.php';
include 'header.php';
require 'auth.php';
redirect_if_not_logged_in();
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$message = '';
$lockedTournament = $_SESSION['locked_tournament'] ?? null;
$username = $_SESSION['username'];
// Get logged-in user's ID
$stmt = $conn->prepare("SELECT id FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->bind_result($loggedInUserId);
$stmt->fetch();
$stmt->close();
// Fetch tournaments where the user is either the creator or moderator
$tournamentsQuery = "
SELECT id, name FROM tournaments
WHERE created_by = ? OR moderated_by = ?";
$tournaments = $conn->prepare($tournamentsQuery);
$tournaments->bind_param("ii", $loggedInUserId, $loggedInUserId);
$tournaments->execute();
$tournamentResult = $tournaments->get_result();
$tournaments->close();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['lock_tournament']) && isset($_POST['tournament_id'])) {
$lockedTournament = intval($_POST['tournament_id']);
$_SESSION['locked_tournament'] = $lockedTournament;
$stmt = $conn->prepare("SELECT name FROM tournaments WHERE id = ?");
$stmt->bind_param("i", $lockedTournament);
$stmt->execute();
$stmt->bind_result($lockedTournamentName);
$stmt->fetch();
$_SESSION['locked_tournament_name'] = $lockedTournamentName;
$stmt->close();
} elseif (isset($_POST['unlock_tournament'])) {
unset($_SESSION['locked_tournament'], $_SESSION['locked_tournament_name']);
$lockedTournament = null;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_match'])) {
$categoryId = intval($_POST['category_id']);
$player1Id = intval($_POST['player1_id']);
$player2Id = intval($_POST['player2_id']);
$stage = $_POST['stage'];
$matchDate = $_POST['date'];
$matchTime = $_POST['match_time'];
$set1P1 = intval($_POST['set1_player1_points']);
$set1P2 = intval($_POST['set1_player2_points']);
$set2P1 = intval($_POST['set2_player1_points']);
$set2P2 = intval($_POST['set2_player2_points']);
$set3P1 = isset($_POST['set3_player1_points']) ? intval($_POST['set3_player1_points']) : null;
$set3P2 = isset($_POST['set3_player2_points']) ? intval($_POST['set3_player2_points']) : null;
if ($categoryId && $player1Id && $player2Id && $stage && $matchDate && $matchTime) {
// Ensure players are not the same
if ($player1Id === $player2Id) {
$message = "Players cannot be the same.";
} else {
$stmt = $conn->prepare("
INSERT INTO matches
(tournament_id, category_id, player1_id, player2_id, stage, match_date, match_time,
set1_player1_points, set1_player2_points, set2_player1_points, set2_player2_points,
set3_player1_points, set3_player2_points)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
");
if ($stmt) {
$stmt->bind_param("iiiissiiiiiii",
$lockedTournament, $categoryId, $player1Id, $player2Id, $stage,
$matchDate, $matchTime,
$set1P1, $set1P2, $set2P1, $set2P2,
$set3P1, $set3P2
);
if ($stmt->execute()) {
$message = "Match successfully added!";
} else {
$message = "Error inserting match: " . $stmt->error;
}
$stmt->close();
} else {
$message = "SQL Prepare Error: " . $conn->error;
}
}
} else {
$message = "All fields are required!";
}
}
// Fetch only BS & GS categories from the locked tournament
$categories = [];
if ($lockedTournament) {
$stmt = $conn->prepare("
SELECT c.id, c.name, c.age_group, c.sex
FROM categories c
INNER JOIN tournament_categories tc ON c.id = tc.category_id
WHERE tc.tournament_id = ? AND (c.name LIKE '%BS%' OR c.name LIKE '%GS%')
");
$stmt->bind_param("i", $lockedTournament);
$stmt->execute();
$categories = $stmt->get_result();
$stmt->close();
}
// Fetch all players
$players = [];
$playerQuery = "SELECT id, name, age, sex FROM players";
$playerResult = $conn->query($playerQuery);
while ($row = $playerResult->fetch_assoc()) {
$players[] = $row;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert Singles Match</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.container {
max-width: 600px;
margin: 20px auto;
padding: 20px;
background: white;
border-radius: 8px;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: #444;
}
label {
display: block;
margin: 10px 0 5px;
font-weight: bold;
}
select, input, button {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
background-color: #007bff;
color: white;
font-size: 16px;
border: none;
cursor: pointer;
padding: 12px;
}
button.locked {
background-color: #28a745;
}
button:hover {
background-color: #0056b3;
}
.message {
text-align: center;
margin-bottom: 20px;
color: #28a745;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h1>Insert Singles Match</h1>
<?php if ($message): ?>
<p class="message"><?= htmlspecialchars($message) ?></p>
<?php endif; ?>
<form method="post">
<label for="tournament_id">Select Tournament:</label>
<select name="tournament_id" id="tournament_id" required <?= $lockedTournament ? 'disabled' : '' ?>>
<option value="">Select Tournament</option>
<?php while ($row = $tournamentResult->fetch_assoc()): ?>
<option value="<?= $row['id'] ?>" <?= ($lockedTournament == $row['id']) ? 'selected' : '' ?>>
<?= htmlspecialchars($row['name']) ?>
</option>
<?php endwhile; ?>
</select>
<?php if ($lockedTournament): ?>
<button type="submit" name="unlock_tournament" style="background-color: red;">Unlock Tournament</button>
<?php else: ?>
<button type="submit" name="lock_tournament">Lock Tournament</button>
<?php endif; ?>
</form>
<?php if ($lockedTournament): ?>
<form method="post">
<label for="category_id">Category:</label>
<select name="category_id" id="category_id" onchange="updatePlayerDropdown()" required>
<option value="">Select Category</option>
<?php while ($row = $categories->fetch_assoc()): ?>
<option value="<?= $row['id'] ?>" data-sex="<?= $row['sex'] ?>" data-age="<?= $row['age_group'] ?>">
<?= htmlspecialchars($row['name']) ?>
</option>
<?php endwhile; ?>
</select>
<label for="player1_id">Player 1:</label>
<select name="player1_id" id="player1_id" required></select>
<label for="player2_id">Player 2:</label>
<select name="player2_id" id="player2_id" required></select>
<label for="stage">Match Stage:</label>
<select name="stage" required>
<option value="Pre Quarter Finals">Pre Quarter Finals</option>
<option value="Quarter Finals">Quarter Finals</option>
<option value="Semi Finals">Semi Finals</option>
<option value="Finals">Finals</option>
</select>
<label for="date">Match Date:</label>
<input type="date" name="date" required>
<label for="match_time">Match Time (12-hour format AM/PM):</label>
<input type="time" name="match_time" required>
<label>Set 1:</label>
<input type="number" name="set1_player1_points" placeholder="Player 1 Score" required>
<input type="number" name="set1_player2_points" placeholder="Player 2 Score" required>
<label>Set 2:</label>
<input type="number" name="set2_player1_points" placeholder="Player 1 Score" required>
<input type="number" name="set2_player2_points" placeholder="Player 2 Score" required>
<label>Set 3:</label>
<input type="number" name="set3_player1_points" placeholder="Player 1 Score">
<input type="number" name="set3_player2_points" placeholder="Player 2 Score">
<button type="submit" name="add_match">Add Match</button>
</form>
<?php endif; ?>
</div>
<script>
const players = <?= json_encode($players) ?>;
function updatePlayerDropdown() {
const category = document.getElementById('category_id');
const player1Dropdown = document.getElementById('player1_id');
const player2Dropdown = document.getElementById('player2_id');
const selectedCategory = category.options[category.selectedIndex];
const categorySex = selectedCategory.dataset.sex;
const categoryAge = selectedCategory.dataset.age;
let maxAge = 100;
if (categoryAge.includes("Under")) {
maxAge = parseInt(categoryAge.replace(/\D/g, ''), 10);
} else if (categoryAge.includes("Plus") || categoryAge.includes("+")) {
maxAge = parseInt(categoryAge.replace(/\D/g, ''), 10);
}
player1Dropdown.innerHTML = '<option value="">Select Player 1</option>';
player2Dropdown.innerHTML = '<option value="">Select Player 2</option>';
players.forEach(player => {
if (player.sex === categorySex && player.age < maxAge) {
const option = `<option value="${player.id}">${player.name} (${player.age}, ${player.sex})</option>`;
player1Dropdown.innerHTML += option;
player2Dropdown.innerHTML += option;
}
});
}
</script>
</body>
</html>