alert('กรุณาเข้าสู่ระบบ'); window.location='../login.php';"; exit; } include '../inc/connect.php'; include 'header.php'; // ฟังก์ชันสำหรับดึงข้อมูลจากตารางประเภทต่างๆ function getTypeData($table, $pdo) { try { $sql = "SELECT id, title FROM `{$table}` WHERE status = 1 ORDER BY id"; $stmt = $pdo->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); // ถ้าไม่พบข้อมูลใน status = 1 ให้ลองดูทั้งหมด if (empty($result)) { $sql = "SELECT id, title FROM `{$table}` ORDER BY id"; $stmt = $pdo->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); } return $result; } catch (PDOException $e) { return array(); } } // ดึงข้อมูลประเภทต่างๆ จากฐานข้อมูล $prefixs = getTypeData('prefix', $pdo); $genders = getTypeData('typeSex', $pdo); $occupations = getTypeData('typeOccupation', $pdo); $statuses = getTypeData('typeStatus', $pdo); $educations = getTypeData('typeEducational', $pdo); $relationships = getTypeData('typeRelationship', $pdo); $helpTypes = getTypeData('typeGiveHelp', $pdo); $serviceTypes = getTypeData('typeService', $pdo); $socialAspects = getTypeData('typesocialSspects', $pdo); $eduOccupations = getTypeData('typeEduOccupation', $pdo); // ดึงข้อมูลจากตาราง success_types (ไม่มีคอลัมน์ status) try { $sql = "SELECT id, title FROM typeServiceSuccess ORDER BY id"; $stmt = $pdo->prepare($sql); $stmt->execute(); $successTypes = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $successTypes = array(); } // ดึงข้อมูลจากตาราง appointments (ไม่มีคอลัมน์ status) try { $sql = "SELECT id, title FROM typeAppointment ORDER BY id"; $stmt = $pdo->prepare($sql); $stmt->execute(); $appointments = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $appointments = array(); } // ดึงข้อมูล sub-menu สำหรับ typeGiveHelp $helpSubTypes = array(); try { $stmt = $pdo->prepare("SELECT id, title, typeGiveHelpId FROM typegivehelpsub WHERE status = 1 ORDER BY typeGiveHelpId, id"); $stmt->execute(); $helpSubData = $stmt->fetchAll(PDO::FETCH_ASSOC); // จัดกลุ่ม sub-types ตาม parent ID foreach ($helpSubData as $sub) { $helpSubTypes[$sub['typeGiveHelpId']][] = $sub; } } catch (PDOException $e) { $helpSubTypes = array(); } // ตรวจสอบว่าเป็นการดูข้อมูลหรือไม่ $editData = null; $serviceData = null; $isView = false; if (isset($_GET['id']) && isset($_GET['mode']) && $_GET['mode'] == 'view') { $editId = intval($_GET['id']); // ตรวจสอบความถูกต้องของ ID if ($editId <= 0) { echo ""; exit; } try { // ดึงข้อมูลจากตาราง individual_counseling $stmt = $pdo->prepare("SELECT * FROM individual_counseling WHERE id = ? AND username = ? AND schoolID = ?"); $stmt->execute(array($editId, $username, $schoolID)); $editData = $stmt->fetch(PDO::FETCH_ASSOC); if ($editData) { $isView = true; // ดึงข้อมูลจากตาราง individual_counseling_service (ถ้ามี) $stmt2 = $pdo->prepare("SELECT * FROM individual_counseling_service WHERE individual_counseling_id = ? ORDER BY id DESC LIMIT 1"); $stmt2->execute(array($editId)); $serviceData = $stmt2->fetch(PDO::FETCH_ASSOC); // รวมข้อมูลจากทั้ง 2 ตาราง if ($serviceData) { $editData = array_merge($editData, $serviceData); } // ดึงประวัติการให้บริการจากฐานข้อมูล $serviceHistory = array(); try { // ลองดึงจากตาราง individual_counseling_service ก่อน $stmt3 = $pdo->prepare("SELECT * FROM individual_counseling_service WHERE individual_counseling_id = ? ORDER BY service_request_date DESC, created_at DESC"); $stmt3->execute(array($editId)); $serviceHistory = $stmt3->fetchAll(PDO::FETCH_ASSOC); // ถ้าไม่มีข้อมูลในตาราง individual_counseling_service ให้ดึงจากตาราง individual_counseling_service if (empty($serviceHistory)) { $stmt4 = $pdo->prepare("SELECT 1 as service_request_number, service_request_date, service_request_details, service_success_id, service_failure_reason, follow_up_id, follow_up_date, created_at FROM individual_counseling_service WHERE individual_counseling_id = ? ORDER BY created_at DESC"); $stmt4->execute(array($editId)); $serviceHistory = $stmt4->fetchAll(PDO::FETCH_ASSOC); } // ถ้ายังไม่มี ลองใช้ข้อมูลจาก individual_counseling โดยตรง if (empty($serviceHistory) && !empty($editData['service_request_date'])) { $serviceHistory = array(array( 'service_request_number' => 1, 'service_request_date' => $editData['service_request_date'], 'service_request_details' => isset($editData['service_request_details']) ? $editData['service_request_details'] : '', 'service_success_id' => isset($editData['service_success_id']) ? $editData['service_success_id'] : null, 'service_failure_reason' => isset($editData['service_failure_reason']) ? $editData['service_failure_reason'] : '', 'follow_up_id' => isset($editData['follow_up_id']) ? $editData['follow_up_id'] : null, 'follow_up_date' => isset($editData['follow_up_date']) ? $editData['follow_up_date'] : '', 'created_at' => isset($editData['created_at']) ? $editData['created_at'] : '' )); } } catch (PDOException $e) { $serviceHistory = array(); // เพิ่มการแสดงข้อผิดพลาดเพื่อดีบัก (สามารถลบได้หลังจากแก้ไขเสร็จ) // error_log("Error fetching service history: " . $e->getMessage()); } } } catch (PDOException $e) { $editData = null; $serviceData = null; $isView = false; } } // ฟังก์ชันสำหรับหาชื่อจาก ID function getNameById($id, $data) { foreach ($data as $item) { if ($item['id'] == $id) { return $item['title']; } } return 'ไม่ระบุ'; } ?>
บริการให้คำปรึกษารายบุคคล
| ครั้งที่ | วันที่ | รายละเอียดสังเขป | ผลการให้บริการ | ดูรายละเอียด |
|---|---|---|---|---|
|
|
สำเร็จ ไม่สำเร็จ ไม่ระบุ |