'กรุณาเข้าสู่ระบบ']); exit; } // ตรวจสอบ service_id $service_id = isset($_GET['service_id']) ? intval($_GET['service_id']) : 0; $person_id = isset($_GET['person_id']) ? intval($_GET['person_id']) : 0; if ($service_id <= 0 || $person_id <= 0) { http_response_code(400); echo json_encode(['error' => 'ไม่พบข้อมูล']); exit; } // ฟังก์ชันสำหรับดึงข้อมูลจากตารางประเภทต่างๆ 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); 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(); } } // ดึงข้อมูลประเภทต่างๆ จากฐานข้อมูล $educations = getTypeData('typeEducational', $pdo); $helpTypes = getTypeData('typeGiveHelp', $pdo); $socialAspects = getTypeData('typesocialSspects', $pdo); $eduOccupations = getTypeData('typeEduOccupation', $pdo); // ดึงข้อมูล 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); foreach ($helpSubData as $sub) { $helpSubTypes[$sub['typeGiveHelpId']][] = $sub; } } catch (PDOException $e) { $helpSubTypes = array(); } // ดึงข้อมูลบุคคล $personData = array(); try { $stmt = $pdo->prepare("SELECT * FROM `individual_counseling` WHERE id = ? AND username = ? AND schoolID = ?"); $stmt->execute(array($person_id, $username, $schoolID)); $personData = $stmt->fetch(PDO::FETCH_ASSOC); if (!$personData) { http_response_code(404); echo json_encode(['error' => 'ไม่พบข้อมูลบุคคล หรือไม่มีสิทธิ์เข้าถึง']); exit; } } catch (PDOException $e) { http_response_code(500); echo json_encode(['error' => 'เกิดข้อผิดพลาด: ' . $e->getMessage()]); exit; } // ดึงข้อมูลการให้บริการ $serviceData = array(); try { $stmt = $pdo->prepare("SELECT * FROM `individual_counseling_service` WHERE id = ? AND individual_counseling_id = ?"); $stmt->execute(array($service_id, $person_id)); $serviceData = $stmt->fetch(PDO::FETCH_ASSOC); if (!$serviceData) { http_response_code(404); echo json_encode(['error' => 'ไม่พบข้อมูลการให้บริการ']); exit; } } catch (PDOException $e) { http_response_code(500); echo json_encode(['error' => 'เกิดข้อผิดพลาด: ' . $e->getMessage()]); exit; } // Return JSON response header('Content-Type: application/json; charset=utf-8'); $response = array( 'success' => true, 'personData' => $personData, 'serviceData' => $serviceData, 'educations' => $educations, 'socialAspects' => $socialAspects, 'eduOccupations' => $eduOccupations, 'helpTypes' => $helpTypes, 'helpSubTypes' => $helpSubTypes ); echo json_encode($response, JSON_UNESCAPED_UNICODE); exit; ?>