Gestionnaire de fichiers - Editer - /home/kridsana/webapp.cm.in.th/663012801/u66301280015/Final/participants.php
Arrière
<?php error_reporting(E_ALL); ini_set('display_errors', 1); session_start(); require_once 'config/database.php'; require_once 'includes/functions.php'; checkLogin(); class StudentManager { private $db; public function __construct($db_connection) { $this->db = $db_connection; } public function getAllStudents() { $query = "SELECT studentCode, firstname, surname, groupCode, gradeNameTh, groupAbbr, groupName FROM student WHERE 1"; $result = $this->db->query($query); $students = []; while ($row = $result->fetch_assoc()) { $students[] = $row; } return $students; } public function addStudentToActivity($activity_id, $student_data, $is_required) { // ตรวจสอบว่าการเชื่อมต่อฐานข้อมูลถูกต้อง if (!$this->db) { die("Database connection failed"); } // ตรวจสอบว่านักเรียนมีอยู่ในฐานข้อมูลหรือไม่ $check_query = "SELECT participant_id FROM participants WHERE student_code = ?"; $stmt = $this->db->prepare($check_query); if (!$stmt) { die("Error in prepare(): " . $this->db->error); } $stmt->bind_param("s", $student_data['studentCode']); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows == 0) { // เพิ่มนักเรียนลงในตาราง participants $insert_participant = "INSERT INTO participants (student_code, first_name, last_name, grade, group_code, group_name) VALUES (?, ?, ?, ?, ?, ?)"; $stmt = $this->db->prepare($insert_participant); if (!$stmt) { die("Error in prepare (Insert Participants): " . $this->db->error); } $stmt->bind_param("ssssss", $student_data['studentCode'], $student_data['firstname'], $student_data['surname'], $student_data['gradeNameTh'], $student_data['groupCode'], $student_data['groupName'] ); if (!$stmt->execute()) { die("Error in execute (Insert Participants): " . $stmt->error); } $participant_id = $this->db->insert_id; } else { $row = $result->fetch_assoc(); $participant_id = $row['participant_id']; } // เพิ่มข้อมูลลงใน activity_participants $query = "INSERT INTO activity_participants (activity_id, participant_id, is_required, attendance_status) VALUES (?, ?, ?, 'ต้องเข้าร่วม')"; $stmt = $this->db->prepare($query); if (!$stmt) { die("Error in prepare (Insert Activity Participants): " . $this->db->error); } $stmt->bind_param("iii", $activity_id, $participant_id, $is_required); if (!$stmt->execute()) { die("Error in execute (Insert Activity Participants): " . $stmt->error); } return true; } } $database = new Database(); $db = $database->getConnection(); $activityManager = new ActivityManager($db); $studentManager = new StudentManager($db); $activity_id = $_GET['activity_id'] ?? null; if (!$activity_id) { header('Location: activity_attendance.php'); exit(); } // ย้ายการดึงข้อมูลนักเรียนขึ้นมาก่อน $students = $studentManager->getAllStudents(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['action'])) { switch ($_POST['action']) { case 'add_participant': $student_code = $_POST['participant_id']; $is_required = isset($_POST['is_required']) ? 1 : 0; // Find the student data $student = null; foreach ($students as $s) { if ($s['studentCode'] === $student_code) { $student = $s; break; } } if ($student) { $studentManager->addStudentToActivity($activity_id, $student, $is_required); } break; case 'remove_participant': $activityManager->removeParticipantFromActivity( $activity_id, $_POST['participant_id'] ); break; case 'update_status': $activityManager->updateAttendanceStatus( $activity_id, $_POST['participant_id'], $_POST['status'] ); break; } } } // ดึงข้อมูลหลังจากมีการเปลี่ยนแปลง $participants = $activityManager->getActivityParticipants($activity_id); $activity = $activityManager->getActivity($activity_id); ?> <!DOCTYPE html> <html lang="th"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>จัดการผู้เข้าร่วมกิจกรรม</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" rel="stylesheet"> <link href="./Css/style.css" rel="stylesheet"> </head> <body> <nav class="navbar"> <div class="nav-containercd"> <a href="Admin_Page.php" class="nav-brand"> ระบบจัดการ </a> <div class="nav-menucd"> <a href="student_attendance.php" class="nav-link"> ระบบเช็คชื่อนักเรียน </a> <a href="student_attendance_report.php" class="nav-link"> รายงานผลการเช็คชื่อ </a> <a href="activity_attendance.php" class="nav-link active"> ระบบเช็คชื่อเข้ากิจกรรม </a> <a href="Logout.php" class="nav-link" style="background-color: #e74c3c;"> ออกจากระบบ </a> </div> </div> </nav> <div class="container mt-4"> <h2>=========</h2> <!-- เพิ่มผู้เข้าร่วม --> <div class="card mb-4"> <div class="card-header"> เพิ่มผู้เข้าร่วม </div> <div class="card-body"> <form method="POST"> <input type="hidden" name="action" value="add_participant"> <div class="mb-3"> <label class="form-label">เลือกผู้เข้าร่วม</label> <select name="participant_id" class="form-control select2" required> <option value="" disabled selected>กรุณาเลือกผู้เข้าร่วม</option> <?php foreach ($students as $student): ?> <option value="<?php echo htmlspecialchars($student['studentCode']); ?>"> <?php echo htmlspecialchars($student['studentCode'] . ' - ' . $student['firstname'] . ' ' . $student['surname'] . ' (' . $student['gradeNameTh'] . ' ' . $student['groupAbbr'] . ')'); ?> </option> <?php endforeach; ?> </select> </div> <div class="mb-3 form-check"> <input type="checkbox" class="form-check-input" name="is_required" id="is_required"> <label class="form-check-label" for="is_required">บังคับเข้าร่วม</label> </div> <button type="submit" class="btn btn-primary">เพิ่มผู้เข้าร่วม</button> </form> </div> </div> <!-- รายชื่อผู้เข้าร่วม --> <div class="card"> <div class="card-header"> รายชื่อผู้เข้าร่วม </div> <div class="card-body"> <table class="table"> <thead> <tr> <th>รหัสนักเรียน</th> <th>ชื่อ-นามสกุล</th> <th>ระดับชั้น</th> <th>ห้อง</th> <th>บังคับเข้าร่วม</th> <th>การจัดการ</th> </tr> </thead> <tbody> <?php foreach ($participants as $participant): ?> <tr> <td><?php echo htmlspecialchars($participant['student_code']); ?></td> <td><?php echo htmlspecialchars($participant['first_name'] . ' ' . $participant['last_name']); ?></td> <td><?php echo htmlspecialchars($participant['grade']); ?></td> <td><?php echo htmlspecialchars($participant['group_name']); ?></td> <td><?php echo $participant['is_required'] ? 'บังคับ' : 'ไม่บังคับ'; ?></td> <td> <form method="POST" style="display: inline;"> <input type="hidden" name="action" value="remove_participant"> <input type="hidden" name="participant_id" value="<?php echo $participant['participant_id']; ?>"> <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('ยืนยันการลบ?')"> ลบ </button> </form> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.min.js"></script> <script> $(document).ready(function() { // Initialize Select2 on the participant dropdown $('.select2').select2({ placeholder: 'ค้นหานักเรียน...', allowClear: true // Optionally allows the user to clear the selection }); }); </script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script> <script> function updateStatus(participantId, status) { const form = document.createElement('form'); form.method = 'POST'; const actionInput = document.createElement('input'); actionInput.type = 'hidden'; actionInput.name = 'action'; actionInput.value = 'update_status'; const participantInput = document.createElement('input'); participantInput.type = 'hidden'; participantInput.name = 'participant_id'; participantInput.value = participantId; const statusInput = document.createElement('input'); statusInput.type = 'hidden'; statusInput.name = 'status'; statusInput.value = status; form.appendChild(actionInput); form.appendChild(participantInput); form.appendChild(statusInput); document.body.appendChild(form); form.submit(); } </script> </body> </html>
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Génération de la page: 0.28 |
proxy
|
phpinfo
|
Réglages