This commit is contained in:
Flatlogic Bot 2025-09-24 13:52:45 +00:00
parent e6e98182ba
commit 6915e64858
2 changed files with 31 additions and 4 deletions

View File

@ -5,6 +5,8 @@ require_once 'db/config.php';
// Handle form submission for adding a new course // Handle form submission for adding a new course
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_course'])) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_course'])) {
$courseName = trim($_POST['courseName']); $courseName = trim($_POST['courseName']);
$hole_count = filter_input(INPUT_POST, 'hole_count', FILTER_VALIDATE_INT);
$tournament_date = $_POST['tournament_date'];
$pars = []; $pars = [];
$isValid = true; $isValid = true;
@ -13,6 +15,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_course'])) {
$isValid = false; $isValid = false;
} }
if (empty($tournament_date)) {
$tournament_date = null;
}
for ($i = 1; $i <= 18; $i++) { for ($i = 1; $i <= 18; $i++) {
$par_value = filter_input(INPUT_POST, 'par_hole_' . $i, FILTER_VALIDATE_INT); $par_value = filter_input(INPUT_POST, 'par_hole_' . $i, FILTER_VALIDATE_INT);
if ($par_value === false || $par_value < 1 || $par_value > 7) { if ($par_value === false || $par_value < 1 || $par_value > 7) {
@ -26,9 +32,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_course'])) {
if ($isValid) { if ($isValid) {
try { try {
$pdo = db(); $pdo = db();
$sql = "INSERT INTO courses (name, par_hole_1, par_hole_2, par_hole_3, par_hole_4, par_hole_5, par_hole_6, par_hole_7, par_hole_8, par_hole_9, par_hole_10, par_hole_11, par_hole_12, par_hole_13, par_hole_14, par_hole_15, par_hole_16, par_hole_17, par_hole_18) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $sql = "INSERT INTO courses (name, hole_count, tournament_date, par_hole_1, par_hole_2, par_hole_3, par_hole_4, par_hole_5, par_hole_6, par_hole_7, par_hole_8, par_hole_9, par_hole_10, par_hole_11, par_hole_12, par_hole_13, par_hole_14, par_hole_15, par_hole_16, par_hole_17, par_hole_18) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $pdo->prepare($sql); $stmt = $pdo->prepare($sql);
$stmt->execute(array_merge([$courseName], $pars)); $params = array_merge([$courseName, $hole_count, $tournament_date], $pars);
$stmt->execute($params);
$_SESSION['success_message'] = "Course '{$courseName}' added successfully!"; $_SESSION['success_message'] = "Course '{$courseName}' added successfully!";
header("Location: admin.php"); header("Location: admin.php");
exit; exit;
@ -41,7 +48,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_course'])) {
// Fetch existing courses // Fetch existing courses
try { try {
$pdo = db(); $pdo = db();
$stmt = $pdo->query("SELECT id, name FROM courses ORDER BY name"); $stmt = $pdo->query("SELECT id, name, hole_count, tournament_date FROM courses ORDER BY name");
$courses = $stmt->fetchAll(); $courses = $stmt->fetchAll();
} catch (PDOException $e) { } catch (PDOException $e) {
$courses = []; $courses = [];
@ -125,6 +132,20 @@ try {
<label for="courseName" class="form-label">Course Name</label> <label for="courseName" class="form-label">Course Name</label>
<input type="text" class="form-control" id="courseName" name="courseName" placeholder="e.g., Pebble Beach" required> <input type="text" class="form-control" id="courseName" name="courseName" placeholder="e.g., Pebble Beach" required>
</div> </div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="hole_count" class="form-label">Number of Holes</label>
<select class="form-select" id="hole_count" name="hole_count">
<option value="9">9 Holes</option>
<option value="18" selected>18 Holes</option>
</select>
</div>
<div class="col-md-6 mb-3">
<label for="tournament_date" class="form-label">Tournament Date</label>
<input type="date" class="form-control" id="tournament_date" name="tournament_date">
</div>
</div>
<h5 class="mt-3">Par Values</h5>
<div class="row"> <div class="row">
<?php for ($i = 1; $i <= 18; $i++): ?> <?php for ($i = 1; $i <= 18; $i++): ?>
<div class="col-md-2 col-sm-4 mb-3"> <div class="col-md-2 col-sm-4 mb-3">
@ -144,16 +165,20 @@ try {
<thead> <thead>
<tr> <tr>
<th>Course Name</th> <th>Course Name</th>
<th>Holes</th>
<th>Tournament Date</th>
<th>Action</th> <th>Action</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php if (empty($courses)): ?> <?php if (empty($courses)): ?>
<tr><td colspan="2">No courses found.</td></tr> <tr><td colspan="4">No courses found.</td></tr>
<?php else: ?> <?php else: ?>
<?php foreach ($courses as $course): ?> <?php foreach ($courses as $course): ?>
<tr> <tr>
<td><?php echo htmlspecialchars($course['name']); ?></td> <td><?php echo htmlspecialchars($course['name']); ?></td>
<td><?php echo htmlspecialchars($course['hole_count']); ?></td>
<td><?php echo htmlspecialchars($course['tournament_date'] ? date('M j, Y', strtotime($course['tournament_date'])) : 'N/A'); ?></td>
<td><button class="btn btn-sm btn-danger">Delete</button></td> <td><button class="btn btn-sm btn-danger">Delete</button></td>
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>

View File

@ -25,6 +25,8 @@ try {
par_hole_16 INT NOT NULL, par_hole_16 INT NOT NULL,
par_hole_17 INT NOT NULL, par_hole_17 INT NOT NULL,
par_hole_18 INT NOT NULL, par_hole_18 INT NOT NULL,
hole_count INT NOT NULL DEFAULT 18,
tournament_date DATE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) )
"); ");