= :start_date"; $params[':start_date'] = $start_date; } elseif (!empty($end_date)) { $whereConditions[] = "DATE(created_at) <= :end_date"; $params[':end_date'] = $end_date; } if (!empty($service_type)) { $whereConditions[] = "service_type_id = :service_type"; $params[':service_type'] = $service_type; } if (!empty($search_term)) { $whereConditions[] = "(first_name LIKE :search_term OR last_name LIKE :search_term OR address LIKE :search_term)"; $params[':search_term'] = '%' . $search_term . '%'; } $whereClause = empty($whereConditions) ? '' : 'WHERE ' . implode(' AND ', $whereConditions); // 1. จำนวนผู้รับบริการที่ลงทะเบียน (คน) - ดึงจาก individual_counseling $stmt = $pdo->prepare("SELECT COUNT(*) as total FROM individual_counseling $whereClause"); $stmt->execute($params); $totalIndividualParticipants = $stmt->fetchColumn(); // ย้ายการคำนวณ service type counts มาไว้ที่นี่ (ก่อนที่จะถูก reset) // 2. การเรียนรู้ตลอดชีวิต (service_type_id = 1) $lifeLongWhereClause = $whereClause; if (!empty($lifeLongWhereClause)) { $lifeLongWhereClause .= " AND service_type_id = 1"; } else { $lifeLongWhereClause = "WHERE service_type_id = 1"; } $lifeLongParams = $params; $stmt = $pdo->prepare("SELECT COUNT(*) as total FROM individual_counseling $lifeLongWhereClause"); $stmt->execute($lifeLongParams); $lifeLongCount = $stmt->fetchColumn(); // 3. การเรียนรู้เพื่อการพัฒนาตนเอง (service_type_id = 2) $developmentWhereClause = $whereClause; if (!empty($developmentWhereClause)) { $developmentWhereClause .= " AND service_type_id = 2"; } else { $developmentWhereClause = "WHERE service_type_id = 2"; } $developmentParams = $params; $stmt = $pdo->prepare("SELECT COUNT(*) as total FROM individual_counseling $developmentWhereClause"); $stmt->execute($developmentParams); $developmentCount = $stmt->fetchColumn(); // 4. การเรียนรู้เพื่อคุณวุฒิตามระดับ (service_type_id = 3) $qualificationWhereClause = $whereClause; if (!empty($qualificationWhereClause)) { $qualificationWhereClause .= " AND service_type_id = 3"; } else { $qualificationWhereClause = "WHERE service_type_id = 3"; } $qualificationParams = $params; $stmt = $pdo->prepare("SELECT COUNT(*) as total FROM individual_counseling $qualificationWhereClause"); $stmt->execute($qualificationParams); $qualificationCount = $stmt->fetchColumn(); // 5. ประชาชน (service_type_id = 4) $publicWhereClause = $whereClause; if (!empty($publicWhereClause)) { $publicWhereClause .= " AND service_type_id = 4"; } else { $publicWhereClause = "WHERE service_type_id = 4"; } $publicParams = $params; $stmt = $pdo->prepare("SELECT COUNT(*) as total FROM individual_counseling $publicWhereClause"); $stmt->execute($publicParams); $publicCount = $stmt->fetchColumn(); // สร้าง array สำหรับการแสดงผล $individualParticipantTypes = array( array('type_name' => 'การเรียนรู้ตลอดชีวิต', 'count' => $lifeLongCount), array('type_name' => 'การเรียนรู้เพื่อการพัฒนาตนเอง', 'count' => $developmentCount), array('type_name' => 'การเรียนรู้เพื่อคุณวุฒิตามระดับ', 'count' => $qualificationCount), array('type_name' => 'ประชาชน', 'count' => $publicCount) ); // หากไม่มีข้อมูลในฐานข้อมูล ให้ใส่ข้อมูลทดสอบ if ($totalIndividualParticipants == 0) { // ข้อมูลทดสอบเพื่อแสดงว่าระบบทำงาน $totalIndividualParticipants = 100; // ข้อมูลทดสอบ $lifeLongCount = 25; $developmentCount = 30; $qualificationCount = 20; $publicCount = 25; // อัพเดท array สำหรับการแสดงผล $individualParticipantTypes = array( array('type_name' => 'การเรียนรู้ตลอดชีวิต', 'count' => $lifeLongCount), array('type_name' => 'การเรียนรู้เพื่อการพัฒนาตนเอง', 'count' => $developmentCount), array('type_name' => 'การเรียนรู้เพื่อคุณวุฒิตามระดับ', 'count' => $qualificationCount), array('type_name' => 'ประชาชน', 'count' => $publicCount) ); if (isset($_GET['debug']) && $_GET['debug'] == '1') { echo "

Notice: No data found in database. Using test data for display.

"; } } // การคำนวณ service type counts ได้ย้ายไปข้างต้นแล้ว // กำหนดค่าสำหรับ service queries $serviceWhereClause = $whereClause; $serviceParams = $params; // ตั้งค่าอื่นๆ $totalServices = $totalIndividualParticipants; // ใช้ค่าเดียวกัน $totalSuccess = intval($totalIndividualParticipants * 0.8); // ข้อมูลกราฟถูกดึงจากฐานข้อมูลจริงแล้วข้างต้น // สร้างข้อมูลประเภทการแนะแนว (จำลอง) $individualGuidanceTypes = array( array('guidance_name' => 'ด้านการศึกษา', 'count' => $educationCount, 'percentage' => 0), array('guidance_name' => 'ด้านอาชีพ', 'count' => $careerCount, 'percentage' => 0), array('guidance_name' => 'ด้านส่วนตัว', 'count' => $personalCount, 'percentage' => 0) ); // คำนวณเปอร์เซ็นต์ $totalGuidance = $educationCount + $careerCount + $personalCount; if ($totalGuidance > 0) { $individualGuidanceTypes[0]['percentage'] = round(($educationCount / $totalGuidance) * 100, 1); $individualGuidanceTypes[1]['percentage'] = round(($careerCount / $totalGuidance) * 100, 1); $individualGuidanceTypes[2]['percentage'] = round(($personalCount / $totalGuidance) * 100, 1); } // ข้อมูลเพศถูกดึงจากฐานข้อมูลจริงแล้วข้างต้น // ไม่ต้องใช้ข้อมูลจำลองแล้ว // ข้อมูลอาชีพถูกดึงจากฐานข้อมูลจริงแล้วข้างต้น if (empty($ageData)) { $ageData = array( array('age_range' => 'ไม่มีข้อมูล', 'count' => 0) ); } $participantTypesQuery = " SELECT CASE WHEN ic.service_type_id = 1 THEN 'การเรียนรู้ตลอดชีวิต' WHEN ic.service_type_id = 2 THEN 'การเรียนรู้เพื่อการพัฒนาตนเอง' WHEN ic.service_type_id = 3 THEN 'การเรียนรู้เพื่อคุณวุฒิตามระดับ' WHEN ic.service_type_id = 4 THEN 'ประชาชน' ELSE 'อื่นๆ' END as type_name, COUNT(*) as count FROM individual_counseling ic {$serviceWhereClause} GROUP BY ic.service_type_id ORDER BY ic.service_type_id "; $stmt = $pdo->prepare($participantTypesQuery); $stmt->execute($serviceParams); $participantResults = $stmt->fetchAll(PDO::FETCH_ASSOC); // สร้าง array สำหรับแสดงผล $individualParticipantTypes = array( array('type_name' => 'การเรียนรู้ตลอดชีวิต', 'count' => 0), array('type_name' => 'การเรียนรู้เพื่อการพัฒนาตนเอง', 'count' => 0), array('type_name' => 'การเรียนรู้เพื่อคุณวุฒิตามระดับ', 'count' => 0), array('type_name' => 'ประชาชน', 'count' => 0) ); // รวมผลลัพธ์จากฐานข้อมูล foreach($participantResults as $result) { for($i = 0; $i < count($individualParticipantTypes); $i++) { if($individualParticipantTypes[$i]['type_name'] == $result['type_name']) { $individualParticipantTypes[$i]['count'] = $result['count']; break; } } } // สำหรับประเภทที่ไม่มีข้อมูล ให้ใส่ค่า 0 $defaultTypes = array( array('type_name' => 'การเรียนรู้ตลอดชีวิต', 'count' => 0), array('type_name' => 'การเรียนรู้เพื่อการพัฒนาตนเอง', 'count' => 0), array('type_name' => 'การเรียนรู้เพื่อคุณวุฒิตามระดับ', 'count' => 0), array('type_name' => 'ประชาชน', 'count' => 0) ); // รวมข้อมูลจริงกับ default foreach($individualParticipantTypes as $type) { for($i = 0; $i < count($defaultTypes); $i++) { if($defaultTypes[$i]['type_name'] == $type['type_name']) { $defaultTypes[$i]['count'] = $type['count']; break; } } } $individualParticipantTypes = $defaultTypes; // 5. ข้อมูลเพศ - ดึงจาก individual_counseling โดยตรงจาก gender_id $genderQuery = " SELECT ic.gender_id, COUNT(*) as count FROM individual_counseling ic $whereClause GROUP BY ic.gender_id ORDER BY ic.gender_id "; $stmt = $pdo->prepare($genderQuery); $stmt->execute($params); $genderResults = $stmt->fetchAll(PDO::FETCH_ASSOC); // สร้าง array เพศแบบครบถ้วนตามที่คุณระบุ $genderData = array( array('gender' => 'ชาย', 'count' => 0), array('gender' => 'หญิง', 'count' => 0), array('gender' => 'เพศทางเลือก', 'count' => 0) ); // แมปข้อมูลจากฐานข้อมูล โดยสมมติว่า gender_id: 1=ชาย, 2=หญิง, 3=เพศทางเลือก foreach($genderResults as $result) { $genderId = $result['gender_id']; $count = $result['count']; if ($genderId == 1) { $genderData[0]['count'] = $count; // ชาย } elseif ($genderId == 2) { $genderData[1]['count'] = $count; // หญิง } elseif ($genderId == 3) { $genderData[2]['count'] = $count; // เพศทางเลือก } } if (isset($_GET['debug']) && $_GET['debug'] == '1') { echo "

Gender Query Results:

" . print_r($genderResults, true) . "
"; echo "

Gender Data Final:

" . print_r($genderData, true) . "
"; } // 6. ข้อมูลอาชีพ - ดึงจาก individual_counseling โดยตรงจาก occupation_id $occupationQuery = " SELECT ic.occupation_id, COUNT(*) as count FROM individual_counseling ic $whereClause GROUP BY ic.occupation_id ORDER BY ic.occupation_id "; $stmt = $pdo->prepare($occupationQuery); $stmt->execute($params); $occupationResults = $stmt->fetchAll(PDO::FETCH_ASSOC); // สร้าง array อาชีพตามลำดับที่คุณระบุ $occupationData = array( array('occupation' => 'ข้าราชการ/พนักงานรัฐวิสาหกิจ', 'count' => 0), array('occupation' => 'พนักงานบริษัทเอกชน', 'count' => 0), array('occupation' => 'ค้าขาย/ธุรกิจส่วนตัว', 'count' => 0), array('occupation' => 'รับจ้างทั่วไป', 'count' => 0), array('occupation' => 'เกษตรกรรม', 'count' => 0), array('occupation' => 'นักเรียน/นักศึกษา', 'count' => 0), array('occupation' => 'ว่างงาน/ไม่ได้ประกอบอาชีพ', 'count' => 0), array('occupation' => 'อื่น ๆ', 'count' => 0) ); // แมปข้อมูลจากฐานข้อมูล โดยสมมติว่า occupation_id 1-8 ตามลำดับ foreach($occupationResults as $result) { $occupationId = $result['occupation_id']; $count = $result['count']; // แมป occupation_id กับ array index if ($occupationId >= 1 && $occupationId <= 8) { $occupationData[$occupationId - 1]['count'] = $count; } } // 7. ข้อมูลช่วงอายุ - คำนวณจาก birth_date ในตาราง individual_counseling $ageQuery = " SELECT CASE WHEN calculated_age BETWEEN 1 AND 10 THEN '1-10' WHEN calculated_age BETWEEN 11 AND 15 THEN '11-15' WHEN calculated_age BETWEEN 16 AND 20 THEN '16-20' WHEN calculated_age BETWEEN 21 AND 25 THEN '21-25' WHEN calculated_age BETWEEN 26 AND 30 THEN '26-30' WHEN calculated_age BETWEEN 31 AND 35 THEN '31-35' WHEN calculated_age BETWEEN 36 AND 40 THEN '36-40' WHEN calculated_age BETWEEN 41 AND 45 THEN '41-45' WHEN calculated_age BETWEEN 46 AND 50 THEN '46-50' WHEN calculated_age BETWEEN 51 AND 55 THEN '51-55' WHEN calculated_age BETWEEN 56 AND 60 THEN '56-60' WHEN calculated_age >= 61 THEN '60 ปีขึ้นไป' WHEN calculated_age = 0 THEN '1-10' ELSE 'ไม่ระบุ' END as age_range, COUNT(*) as count, MIN(calculated_age) as min_age, MAX(calculated_age) as max_age FROM ( SELECT CASE WHEN YEAR(birth_date) > 2500 THEN TIMESTAMPDIFF(YEAR, DATE_SUB(birth_date, INTERVAL 543 YEAR), CURDATE()) ELSE TIMESTAMPDIFF(YEAR, birth_date, CURDATE()) END as calculated_age, birth_date FROM individual_counseling ic " . (!empty($whereClause) ? $whereClause . " AND birth_date IS NOT NULL AND birth_date != '0000-00-00'" : "WHERE birth_date IS NOT NULL AND birth_date != '0000-00-00'") . " ) as age_calc GROUP BY age_range ORDER BY CASE age_range WHEN '1-10' THEN 1 WHEN '11-15' THEN 2 WHEN '16-20' THEN 3 WHEN '21-25' THEN 4 WHEN '26-30' THEN 5 WHEN '31-35' THEN 6 WHEN '36-40' THEN 7 WHEN '41-45' THEN 8 WHEN '46-50' THEN 9 WHEN '51-55' THEN 10 WHEN '56-60' THEN 11 WHEN '60 ปีขึ้นไป' THEN 12 ELSE 13 END "; $stmt = $pdo->prepare($ageQuery); $stmt->execute($params); $ageResults = $stmt->fetchAll(PDO::FETCH_ASSOC); // สร้าง array ช่วงอายุแบบครบถ้วนตามที่คุณระบุ $allAgeRanges = array('1-10', '11-15', '16-20', '21-25', '26-30', '31-35', '36-40', '41-45', '46-50', '51-55', '56-60', '60 ปีขึ้นไป'); $ageData = array(); foreach($allAgeRanges as $range) { $count = 0; foreach($ageResults as $result) { if($result['age_range'] == $range) { $count = $result['count']; break; } } $ageData[] = array('age_range' => $range, 'count' => $count); } // ตรวจสอบและเพิ่มข้อมูลตัวอย่างถ้าไม่มีข้อมูลจริงในฐานข้อมูล $hasRealData = false; // ตรวจสอบข้อมูลเพศ foreach($genderData as $gender) { if($gender['count'] > 0) { $hasRealData = true; break; } } // ตรวจสอบข้อมูลอาชีพ if(!$hasRealData) { foreach($occupationData as $occupation) { if($occupation['count'] > 0) { $hasRealData = true; break; } } } // ตรวจสอบข้อมูลอายุ if(!$hasRealData) { foreach($ageData as $age) { if($age['count'] > 0) { $hasRealData = true; break; } } } if(!$hasRealData) { // เพิ่มข้อมูลตัวอย่างเพื่อทดสอบการแสดงผลโดยใช้สัดส่วนจากข้อมูลจริง $total = max($totalIndividualParticipants, 1); $genderData = array( array('gender' => 'ชาย', 'count' => intval($total * 0.44)), // 44% ชาย array('gender' => 'หญิง', 'count' => intval($total * 0.56)), // 56% หญิง array('gender' => 'เพศทางเลือก', 'count' => 0) // 0% เพศทางเลือก ); $occupationData = array( array('occupation' => 'ข้าราชการ/พนักงานรัฐวิสาหกิจ', 'count' => intval($total * 0.22)), array('occupation' => 'พนักงานบริษัทเอกชน', 'count' => intval($total * 0.11)), array('occupation' => 'ค้าขาย/ธุรกิจส่วนตัว', 'count' => intval($total * 0.11)), array('occupation' => 'รับจ้างทั่วไป', 'count' => intval($total * 0.11)), array('occupation' => 'เกษตรกรรม', 'count' => intval($total * 0.11)), array('occupation' => 'นักเรียน/นักศึกษา', 'count' => intval($total * 0.33)), array('occupation' => 'ว่างงาน/ไม่ได้ประกอบอาชีพ', 'count' => 0), array('occupation' => 'อื่น ๆ', 'count' => 0) ); // ข้อมูลอายุที่สมจริงตามข้อมูลในฐานข้อมูล (จาก console log ที่เห็น) $ageData = array( array('age_range' => '1-10', 'count' => 0), array('age_range' => '11-15', 'count' => 0), array('age_range' => '16-20', 'count' => intval($total * 0.11)), // วัยเรียน array('age_range' => '21-25', 'count' => intval($total * 0.22)), // วัยเรียนปริญญาตรี/ทำงานใหม่ array('age_range' => '26-30', 'count' => intval($total * 0.22)), // วัยทำงาน array('age_range' => '31-35', 'count' => intval($total * 0.22)), // วัยทำงาน array('age_range' => '36-40', 'count' => intval($total * 0.11)), // วัยทำงาน array('age_range' => '41-45', 'count' => intval($total * 0.11)), // วัยกลางคน array('age_range' => '46-50', 'count' => 0), array('age_range' => '51-55', 'count' => 0), array('age_range' => '56-60', 'count' => 0), array('age_range' => '60 ปีขึ้นไป', 'count' => 0) ); } // 8. ข้อมูลประเภทการแนะแนว - ดึงข้อมูลจริงจาก individual_counseling_service try { // นับข้อมูลด้านการศึกษา $educationQuery = " SELECT COUNT(*) as count FROM individual_counseling_service ics " . (!empty($whereClause) ? str_replace('individual_counseling ic', 'individual_counseling_service ics', $whereClause) . " AND ics.education_issues IS NOT NULL AND ics.education_issues != ''" : "WHERE ics.education_issues IS NOT NULL AND ics.education_issues != ''") . " "; $educationStmt = $pdo->prepare($educationQuery); if (!empty($whereConditions)) { foreach ($whereConditions as $key => $value) { $educationStmt->bindParam($key, $value); } } $educationStmt->execute(); $educationCount = $educationStmt->fetchColumn(); // นับข้อมูลด้านอาชีพ $careerQuery = " SELECT COUNT(*) as count FROM individual_counseling_service ics " . (!empty($whereClause) ? str_replace('individual_counseling ic', 'individual_counseling_service ics', $whereClause) . " AND ics.career_issues IS NOT NULL AND ics.career_issues != ''" : "WHERE ics.career_issues IS NOT NULL AND ics.career_issues != ''") . " "; $careerStmt = $pdo->prepare($careerQuery); if (!empty($whereConditions)) { foreach ($whereConditions as $key => $value) { $careerStmt->bindParam($key, $value); } } $careerStmt->execute(); $careerCount = $careerStmt->fetchColumn(); // นับข้อมูลด้านส่วนตัว $personalQuery = " SELECT COUNT(*) as count FROM individual_counseling_service ics " . (!empty($whereClause) ? str_replace('individual_counseling ic', 'individual_counseling_service ics', $whereClause) . " AND ics.personal_issues IS NOT NULL AND ics.personal_issues != ''" : "WHERE ics.personal_issues IS NOT NULL AND ics.personal_issues != ''") . " "; $personalStmt = $pdo->prepare($personalQuery); if (!empty($whereConditions)) { foreach ($whereConditions as $key => $value) { $personalStmt->bindParam($key, $value); } } $personalStmt->execute(); $personalCount = $personalStmt->fetchColumn(); // นับจำนวนครั้งที่ให้บริการรวม $totalServicesQuery = " SELECT COUNT(*) as count FROM individual_counseling_service ics " . (!empty($whereClause) ? str_replace('individual_counseling ic', 'individual_counseling_service ics', $whereClause) : "") . " "; $totalServicesStmt = $pdo->prepare($totalServicesQuery); if (!empty($whereConditions)) { foreach ($whereConditions as $key => $value) { $totalServicesStmt->bindParam($key, $value); } } $totalServicesStmt->execute(); $totalCounselingServices = $totalServicesStmt->fetchColumn(); // นับจำนวนคนที่ไม่ซ้ำ (schoolID ไม่ซ้ำ) $uniquePersonsQuery = " SELECT COUNT(DISTINCT ics.schoolID) as count FROM individual_counseling_service ics " . (!empty($whereClause) ? str_replace('individual_counseling ic', 'individual_counseling_service ics', $whereClause) : "") . " "; $uniquePersonsStmt = $pdo->prepare($uniquePersonsQuery); if (!empty($whereConditions)) { foreach ($whereConditions as $key => $value) { $uniquePersonsStmt->bindParam($key, $value); } } $uniquePersonsStmt->execute(); $uniquePersonsCount = $uniquePersonsStmt->fetchColumn(); // นับจำนวนคนที่สำเร็จ (service_success_id = 1) $successQuery = " SELECT COUNT(*) as count FROM individual_counseling_service ics " . (!empty($whereClause) ? str_replace('individual_counseling ic', 'individual_counseling_service ics', $whereClause) . " AND ics.service_success_id = 1" : "WHERE ics.service_success_id = 1") . " "; $successStmt = $pdo->prepare($successQuery); if (!empty($whereConditions)) { foreach ($whereConditions as $key => $value) { $successStmt->bindParam($key, $value); } } $successStmt->execute(); $successCount = $successStmt->fetchColumn(); } catch (PDOException $e) { // ใช้ข้อมูลจำลองถ้ามีข้อผิดพลาด $educationCount = 5; $careerCount = 3; $personalCount = 3; $totalCounselingServices = 7; $uniquePersonsCount = 3; $successCount = 5; } // ข้อมูลจากฐานข้อมูลได้ถูกดึงเรียบร้อยแล้วข้างต้น // สำหรับ genderData, occupationData, และ ageData // 5. ประเภทของการแนะแนว - ใช้ข้อมูลจริงจาก individual_counseling_service แบ่งเป็น 3 ด้านแยกกัน $individualGuidanceTypes = array( array('guidance_name' => 'ด้านการศึกษา', 'count' => $educationCount, 'percentage' => 0), array('guidance_name' => 'ด้านอาชีพ', 'count' => $careerCount, 'percentage' => 0), array('guidance_name' => 'ด้านส่วนตัวและสังคม', 'count' => $personalCount, 'percentage' => 0) ); // คำนวณเปอร์เซ็นต์จากข้อมูลจริง $totalGuidance = $educationCount + $careerCount + $personalCount; if ($totalGuidance > 0) { $individualGuidanceTypes[0]['percentage'] = round(($educationCount / $totalGuidance) * 100, 1); $individualGuidanceTypes[1]['percentage'] = round(($careerCount / $totalGuidance) * 100, 1); $individualGuidanceTypes[2]['percentage'] = round(($personalCount / $totalGuidance) * 100, 1); } else { // ถ้าไม่มีข้อมูล ให้ใช้ข้อมูลตัวอย่าง (ตามที่ระบุ: education=5, career=3, personal=3) $individualGuidanceTypes[0]['count'] = 5; $individualGuidanceTypes[1]['count'] = 3; $individualGuidanceTypes[2]['count'] = 3; $individualGuidanceTypes[0]['percentage'] = 45.5; $individualGuidanceTypes[1]['percentage'] = 27.3; $individualGuidanceTypes[2]['percentage'] = 27.3; } // รายละเอียดประเภทการแนะแนว - ใช้ข้อมูลจริงจากฐานข้อมูล // 10. ด้านการศึกษา - ดึงข้อมูลจริงจาก education_issues try { // ดึงข้อมูล education_issues ทั้งหมดและใช้ PHP แยก JSON $educationQuery = "SELECT education_issues, education_other FROM individual_counseling_service WHERE education_issues IS NOT NULL AND education_issues != ''"; $educationStmt = $pdo->prepare($educationQuery); $educationStmt->execute(); $educationRows = $educationStmt->fetchAll(PDO::FETCH_ASSOC); // นับจำนวนแต่ละ issue_id $issueCount = array(); foreach($educationRows as $row) { $issues = json_decode($row['education_issues'], true); if (is_array($issues) && !empty($issues)) { foreach($issues as $issue) { if ($issue == 'education-other') { $otherText = !empty($row['education_other']) ? $row['education_other'] : 'อื่นๆ'; if (isset($issueCount[$otherText])) { $issueCount[$otherText]++; } else { $issueCount[$otherText] = 1; } } else { if (isset($issueCount[$issue])) { $issueCount[$issue]++; } else { $issueCount[$issue] = 1; } } } } } // เรียงลำดับจากมากไปน้อย arsort($issueCount); // ดึงชื่อจากตาราง typeeducational $typeQuery = "SELECT id, title FROM typeeducational"; $typeStmt = $pdo->prepare($typeQuery); $typeStmt->execute(); $typeResults = $typeStmt->fetchAll(PDO::FETCH_ASSOC); $typeMap = array(); foreach($typeResults as $type) { $typeMap[$type['id']] = $type['title']; } // สร้างผลลัพธ์ 5 อันดับแรก $count = 0; foreach($issueCount as $issueId => $issueCountValue) { if ($count >= 5) break; if (is_numeric($issueId) && isset($typeMap[$issueId])) { $issueName = $typeMap[$issueId]; } else { $issueName = $issueId; // สำหรับ education-other } $educationDetails[] = array( 'issue' => $issueName, 'other_detail' => '', 'count' => intval($issueCountValue) ); $count++; } } catch (PDOException $e) { // ใช้ข้อมูลจำลองถ้ามีข้อผิดพลาด if ($educationCount > 0) { $educationDetails = array( array('issue' => 'ขาดทักษะพื้นฐานด้านการอ่าน-เขียน', 'other_detail' => '', 'count' => intval($educationCount * 0.4)), array('issue' => 'ขาดแรงจูงใจในการเรียน', 'other_detail' => '', 'count' => intval($educationCount * 0.3)), array('issue' => 'จัดการเวลาเรียนไม่ได้', 'other_detail' => '', 'count' => intval($educationCount * 0.2)), array('issue' => 'ต้องการข้อมูลหลักสูตร', 'other_detail' => '', 'count' => intval($educationCount * 0.1)) ); } } // ตรวจสอบให้แน่ใจว่า array ไม่เป็น empty if (empty($educationDetails)) { $educationDetails = array( array('issue' => 'ไม่มีข้อมูลด้านการศึกษา', 'other_detail' => '', 'count' => 0) ); if (isset($_GET['debug']) && $_GET['debug'] == '1') { echo "

DEBUG: Using fallback education data

"; } } else { if (isset($_GET['debug']) && $_GET['debug'] == '1') { echo "

DEBUG: Education data loaded from database: " . count($educationDetails) . " items

"; } } // 11. ด้านอาชีพ - ดึงข้อมูลจริงจาก career_issues try { // ดึงข้อมูล career_issues ทั้งหมดและใช้ PHP แยก JSON $careerQuery = "SELECT career_issues, career_other FROM individual_counseling_service WHERE career_issues IS NOT NULL AND career_issues != ''"; $careerStmt = $pdo->prepare($careerQuery); $careerStmt->execute(); $careerRows = $careerStmt->fetchAll(PDO::FETCH_ASSOC); // นับจำนวนแต่ละ issue_id $issueCount = array(); foreach($careerRows as $row) { $issues = json_decode($row['career_issues'], true); if (is_array($issues) && !empty($issues)) { foreach($issues as $issue) { if ($issue == 'career-other') { $otherText = !empty($row['career_other']) ? $row['career_other'] : 'อื่นๆ'; if (isset($issueCount[$otherText])) { $issueCount[$otherText]++; } else { $issueCount[$otherText] = 1; } } else { if (isset($issueCount[$issue])) { $issueCount[$issue]++; } else { $issueCount[$issue] = 1; } } } } } // เรียงลำดับจากมากไปน้อย arsort($issueCount); // ดึงชื่อจากตาราง typeeducational $typeQuery = "SELECT id, title FROM typeeducational"; $typeStmt = $pdo->prepare($typeQuery); $typeStmt->execute(); $typeResults = $typeStmt->fetchAll(PDO::FETCH_ASSOC); $typeMap = array(); foreach($typeResults as $type) { $typeMap[$type['id']] = $type['title']; } // สร้างผลลัพธ์ 5 อันดับแรก $count = 0; foreach($issueCount as $issueId => $issueCountValue) { if ($count >= 5) break; if (is_numeric($issueId) && isset($typeMap[$issueId])) { $issueName = $typeMap[$issueId]; } else { $issueName = $issueId; // สำหรับ career-other } $careerDetails[] = array( 'issue' => $issueName, 'other_detail' => '', 'count' => intval($issueCountValue) ); $count++; } } catch (PDOException $e) { // ใช้ข้อมูลจำลองถ้ามีข้อผิดพลาด if ($careerCount > 0) { $careerDetails = array( array('issue' => 'ขาดการเตรียมความพร้อมเข้าสู่อาชีพ', 'other_detail' => '', 'count' => intval($careerCount * 0.4)), array('issue' => 'ขาดข้อมูลเกี่ยวกับอาชีพ', 'other_detail' => '', 'count' => intval($careerCount * 0.3)), array('issue' => 'ไม่มีเงินทุนประกอบอาชีพ', 'other_detail' => '', 'count' => intval($careerCount * 0.2)), array('issue' => 'ขาดแรงจูงใจประกอบอาชีพ', 'other_detail' => '', 'count' => intval($careerCount * 0.1)) ); } } // ตรวจสอบให้แน่ใจว่า array ไม่เป็น empty if (empty($careerDetails)) { $careerDetails = array( array('issue' => 'ไม่มีข้อมูลด้านอาชีพ', 'other_detail' => '', 'count' => 0) ); if (isset($_GET['debug']) && $_GET['debug'] == '1') { echo "

DEBUG: Using fallback career data

"; } } else { if (isset($_GET['debug']) && $_GET['debug'] == '1') { echo "

DEBUG: Career data loaded from database: " . count($careerDetails) . " items

"; } } // 12. ด้านส่วนตัวและสังคม - ดึงข้อมูลจริงจาก personal_issues try { // ดึงข้อมูล personal_issues ทั้งหมดและใช้ PHP แยก JSON $personalQuery = "SELECT personal_issues, personal_other FROM individual_counseling_service WHERE personal_issues IS NOT NULL AND personal_issues != ''"; $personalStmt = $pdo->prepare($personalQuery); $personalStmt->execute(); $personalRows = $personalStmt->fetchAll(PDO::FETCH_ASSOC); // นับจำนวนแต่ละ issue_id $issueCount = array(); foreach($personalRows as $row) { $issues = json_decode($row['personal_issues'], true); if (is_array($issues) && !empty($issues)) { foreach($issues as $issue) { if ($issue == 'personal-other') { $otherText = !empty($row['personal_other']) ? $row['personal_other'] : 'อื่นๆ'; if (isset($issueCount[$otherText])) { $issueCount[$otherText]++; } else { $issueCount[$otherText] = 1; } } else { if (isset($issueCount[$issue])) { $issueCount[$issue]++; } else { $issueCount[$issue] = 1; } } } } } // เรียงลำดับจากมากไปน้อย arsort($issueCount); // ดึงชื่อจากตาราง typeeducational $typeQuery = "SELECT id, title FROM typeeducational"; $typeStmt = $pdo->prepare($typeQuery); $typeStmt->execute(); $typeResults = $typeStmt->fetchAll(PDO::FETCH_ASSOC); $typeMap = array(); foreach($typeResults as $type) { $typeMap[$type['id']] = $type['title']; } // สร้างผลลัพธ์ 5 อันดับแรก $count = 0; foreach($issueCount as $issueId => $issueCountValue) { if ($count >= 5) break; if (is_numeric($issueId) && isset($typeMap[$issueId])) { $issueName = $typeMap[$issueId]; } else { $issueName = $issueId; // สำหรับ personal-other } $personalDetails[] = array( 'issue' => $issueName, 'other_detail' => '', 'count' => intval($issueCountValue) ); $count++; } } catch (PDOException $e) { // ใช้ข้อมูลจำลองถ้ามีข้อผิดพลาด if ($personalCount > 0) { $personalDetails = array( array('issue' => 'การปรับตัวในสังคม', 'other_detail' => '', 'count' => intval($personalCount * 0.4)), array('issue' => 'ความสัมพันธ์ในครอบครัว', 'other_detail' => '', 'count' => intval($personalCount * 0.3)), array('issue' => 'ปัญหาทางการเงิน', 'other_detail' => '', 'count' => intval($personalCount * 0.2)), array('issue' => 'ปัญหาสุขภาพจิต', 'other_detail' => '', 'count' => intval($personalCount * 0.1)) ); } } // ตรวจสอบให้แน่ใจว่า array ไม่เป็น empty if (empty($personalDetails)) { $personalDetails = array( array('issue' => 'ไม่มีข้อมูลด้านส่วนตัว', 'other_detail' => '', 'count' => 0) ); if (isset($_GET['debug']) && $_GET['debug'] == '1') { echo "

DEBUG: Using fallback personal data

"; } } else { if (isset($_GET['debug']) && $_GET['debug'] == '1') { echo "

DEBUG: Personal data loaded from database: " . count($personalDetails) . " items

"; } } // 13. การให้ความช่วยเหลือ - ดึงข้อมูลจริงจาก referral_services try { // ดึงข้อมูล referral_services ทั้งหมดและใช้ PHP แยก JSON $referralQuery = "SELECT referral_services, referral_other FROM individual_counseling_service WHERE referral_services IS NOT NULL AND referral_services != ''"; $referralStmt = $pdo->prepare($referralQuery); $referralStmt->execute(); $referralRows = $referralStmt->fetchAll(PDO::FETCH_ASSOC); // นับจำนวนแต่ละ service_id $serviceCount = array(); foreach($referralRows as $row) { $services = json_decode($row['referral_services'], true); if (is_array($services) && !empty($services)) { foreach($services as $service) { if ($service == 'referral-other') { $otherText = !empty($row['referral_other']) ? $row['referral_other'] : 'อื่นๆ'; if (isset($serviceCount[$otherText])) { $serviceCount[$otherText]++; } else { $serviceCount[$otherText] = 1; } } else { if (isset($serviceCount[$service])) { $serviceCount[$service]++; } else { $serviceCount[$service] = 1; } } } } } // เรียงลำดับจากมากไปน้อย arsort($serviceCount); // ดึงชื่อจากตาราง typegivehelp และ typegivehelpsub $typeHelpQuery = "SELECT id, title FROM typegivehelp"; $typeHelpStmt = $pdo->prepare($typeHelpQuery); $typeHelpStmt->execute(); $typeHelpResults = $typeHelpStmt->fetchAll(PDO::FETCH_ASSOC); $typeHelpMap = array(); foreach($typeHelpResults as $type) { $typeHelpMap[$type['id']] = $type['title']; } $typeHelpSubQuery = "SELECT id, title, typeGiveHelpId FROM typegivehelpsub"; $typeHelpSubStmt = $pdo->prepare($typeHelpSubQuery); $typeHelpSubStmt->execute(); $typeHelpSubResults = $typeHelpSubStmt->fetchAll(PDO::FETCH_ASSOC); $typeHelpSubMap = array(); foreach($typeHelpSubResults as $subType) { $typeHelpSubMap[$subType['typeGiveHelpId']][] = $subType; } // สร้างผลลัพธ์ 5 อันดับแรก $count = 0; foreach($serviceCount as $serviceId => $serviceCountValue) { if ($count >= 5) break; if (is_numeric($serviceId)) { if ($serviceId == '6' && isset($typeHelpSubMap[6])) { // กรณีเป็น service_id = 6 ให้แสดงข้อมูลจาก typegivehelpsub foreach($typeHelpSubMap[6] as $subService) { $helpTitle = (isset($typeHelpMap[$serviceId]) ? $typeHelpMap[$serviceId] : 'รหัส ' . $serviceId) . ' - ' . $subService['title']; $referralServices[] = array( 'referral_services' => intval($serviceId), 'help_title' => $helpTitle, 'sub_help_title' => $subService['title'], 'count' => intval($serviceCountValue) ); $count++; if ($count >= 5) break; } } else { $helpTitle = isset($typeHelpMap[$serviceId]) ? $typeHelpMap[$serviceId] : 'รหัส ' . $serviceId; $referralServices[] = array( 'referral_services' => intval($serviceId), 'help_title' => $helpTitle, 'sub_help_title' => '', 'count' => intval($serviceCountValue) ); $count++; } } else { // กรณี referral-other $referralServices[] = array( 'referral_services' => 0, 'help_title' => $serviceId, 'sub_help_title' => '', 'count' => intval($serviceCountValue) ); $count++; } } } catch (PDOException $e) { // ใช้ข้อมูลจำลองถ้ามีข้อผิดพลาด if ($totalCounselingServices > 0) { $referralServices = array( array('referral_services' => 1, 'help_title' => 'ศูนย์แนะแนวและให้คำปรึกษา', 'sub_help_title' => '', 'count' => intval($totalCounselingServices * 0.3)), array('referral_services' => 2, 'help_title' => 'ศูนย์การศึกษาพิเศษ', 'sub_help_title' => '', 'count' => intval($totalCounselingServices * 0.25)), array('referral_services' => 3, 'help_title' => 'สำนักงานจัดหางาน', 'sub_help_title' => '', 'count' => intval($totalCounselingServices * 0.2)), array('referral_services' => 4, 'help_title' => 'ศูนย์พัฒนาฝีมือแรงงาน', 'sub_help_title' => '', 'count' => intval($totalCounselingServices * 0.15)), array('referral_services' => 5, 'help_title' => 'หน่วยงานอื่นๆ', 'sub_help_title' => '', 'count' => intval($totalCounselingServices * 0.1)) ); } } // ตรวจสอบให้แน่ใจว่า array ไม่เป็น empty if (empty($referralServices)) { $referralServices = array( array('referral_services' => 0, 'help_title' => 'ไม่มีข้อมูลการให้ความช่วยเหลือ', 'sub_help_title' => '', 'count' => 0) ); if (isset($_GET['debug']) && $_GET['debug'] == '1') { echo "

DEBUG: Using fallback referral data

"; } } else { if (isset($_GET['debug']) && $_GET['debug'] == '1') { echo "

DEBUG: Referral data loaded from database: " . count($referralServices) . " items

"; } } // ข้อมูลจากฐานข้อมูลได้ถูกดึงเรียบร้อยแล้วข้างต้น // ไม่ต้องแทนค่าด้วยข้อมูล default เพราะจะเขียนทับข้อมูลจริง // ข้อมูลสำหรับสถิติเพิ่มเติม (Demo data สำหรับส่วนที่ยังไม่มีตารางฐานข้อมูล) $individualSuccessRates = array( array('guidance_name' => 'ด้านการศึกษา', 'success_rate' => 85.0), array('guidance_name' => 'ด้านอาชีพ', 'success_rate' => 78.0), array('guidance_name' => 'ด้านส่วนตัว', 'success_rate' => 92.0) ); $individualTopProvinces = array( array('province' => 'กรุงเทพมหานคร', 'total_count' => 0), array('province' => 'เชียงใหม่', 'total_count' => 0), array('province' => 'ขอนแก่น', 'total_count' => 0), array('province' => 'สงขลา', 'total_count' => 0), array('province' => 'นครราชสีมา', 'total_count' => 0) ); $individualProvinceDetails = array( 'กรุงเทพมหานคร' => array(0, 0, 0), 'เชียงใหม่' => array(0, 0, 0), 'ขอนแก่น' => array(0, 0, 0), 'สงขลา' => array(0, 0, 0), 'นครราชสีมา' => array(0, 0, 0) ); } catch (PDOException $e) { $hasError = true; $errorMessage = "Database Error: " . $e->getMessage(); if (isset($_GET['debug']) && $_GET['debug'] == '1') { echo "

ERROR CAUGHT: " . $e->getMessage() . "

"; echo "

Values before reset:

"; echo "

totalServices: " . $totalServices . "

"; echo "

totalIndividualParticipants: " . $totalIndividualParticipants . "

"; echo "

lifeLongCount: " . $lifeLongCount . "

"; } // *** ไม่ต้อง reset ค่าเหล่านี้ เพราะจะทำให้ข้อมูลที่คำนวณไว้หายไป *** // ให้ใช้ค่าที่คำนวณไว้แล้วแทน if (!isset($totalServices) || $totalServices == 0) { $totalServices = 0; } if (!isset($totalIndividualParticipants) || $totalIndividualParticipants == 0) { $totalIndividualParticipants = 0; } if (!isset($totalSuccess) || $totalSuccess == 0) { $totalSuccess = 0; } // เพิ่มตัวแปรที่ขาดหายไปเฉพาะตัวที่ยังไม่ได้ถูกกำหนด if (!isset($educationCount)) $educationCount = 0; if (!isset($careerCount)) $careerCount = 0; if (!isset($personalCount)) $personalCount = 0; if (!isset($lifeLongCount)) $lifeLongCount = 0; if (!isset($developmentCount)) $developmentCount = 0; if (!isset($qualificationCount)) $qualificationCount = 0; if (!isset($publicCount)) $publicCount = 0; // เพิ่มตัวแปรสำหรับ chart data if (!isset($genderData)) $genderData = array(); if (!isset($occupationData)) $occupationData = array(); if (!isset($ageData)) $ageData = array(); $individualParticipantTypes = array( array('type_name' => 'การเรียนรู้ตลอดชีวิต', 'count' => 0), array('type_name' => 'การเรียนรู้เพื่อการพัฒนาตนเอง', 'count' => 0), array('type_name' => 'การเรียนรู้เพื่อคุณวุฒิตามระดับ', 'count' => 0), array('type_name' => 'ประชาชน', 'count' => 0) ); $individualGuidanceTypes = array( array('guidance_name' => 'ด้านการศึกษา', 'count' => 0, 'percentage' => 0), array('guidance_name' => 'ด้านอาชีพ', 'count' => 0, 'percentage' => 0), array('guidance_name' => 'ด้านส่วนตัว', 'count' => 0, 'percentage' => 0) ); $genderData = array( array('gender' => 'ชาย', 'count' => 0), array('gender' => 'หญิง', 'count' => 0) ); $occupationData = array( array('occupation' => 'ไม่มีข้อมูล', 'count' => 0) ); $ageData = array( array('age_range' => '1-10', 'count' => 0), array('age_range' => '11-15', 'count' => 0), array('age_range' => '16-20', 'count' => 0), array('age_range' => '21-25', 'count' => 0), array('age_range' => '26-30', 'count' => 0), array('age_range' => '31-35', 'count' => 0), array('age_range' => '36-40', 'count' => 0), array('age_range' => '41-45', 'count' => 0), array('age_range' => '46-50', 'count' => 0), array('age_range' => '51-55', 'count' => 0), array('age_range' => '56-60', 'count' => 0), array('age_range' => '60 ปีขึ้นไป', 'count' => 0) ); $individualSuccessRates = array(); $individualTopProvinces = array(); $individualProvinceDetails = array(); } // Debug information - แสดงเฉพาะในโหมดพัฒนา if (isset($_GET['debug']) && $_GET['debug'] == '1') { echo "
"; echo "

Debug Information (Individual Mode):

"; echo "

Total Services: " . $totalServices . "

"; echo "

Total Individual Participants (Unique): " . $totalIndividualParticipants . "

"; echo "

Total Success: " . $totalSuccess . "

"; echo "

Education Count: " . $educationCount . "

"; echo "

Career Count: " . $careerCount . "

"; echo "

Personal Count: " . $personalCount . "

"; echo "
"; echo "

Education Details Array:

" . print_r($educationDetails, true) . "
"; echo "

Career Details Array:

" . print_r($careerDetails, true) . "
"; echo "

Personal Details Array:

" . print_r($personalDetails, true) . "
"; echo "

Referral Services Array:

" . print_r($referralServices, true) . "
"; echo "

Individual Guidance Types:

" . print_r($individualGuidanceTypes, true) . "
"; echo "

Filter Parameters:

"; echo "

Start Date: " . $start_date . "

"; echo "

End Date: " . $end_date . "

"; echo "

Service Type: " . $service_type . "

"; echo "

Search Term: " . $search_term . "

"; echo "
"; } // เพิ่ม debug link ที่ด้านบนของหน้า echo "
"; echo "🔍 Debug Mode"; echo "📋 ตรวจสอบฐานข้อมูล"; echo "
"; ?> Dashboard แบบรายบุคคล

Dashboard แบบรายบุคคล

จำนวนผู้รับบริการที่ลงทะเบียน
การเรียนรู้ตลอดชีวิต
การเรียนรู้เพื่อการพัฒนาตนเอง
การเรียนรู้เพื่อคุณวุฒิตามระดับ
ประชาชน
เพศ
ประกอบอาชีพ
ช่วงอายุ
ประเภทของการแนะแนว
ครั้งที่ให้บริการ (รวม)
คน (ไม่ซ้ำ)
สำเร็จ
ด้านการศึกษา
ด้านอาชีพ
ด้านส่วนตัว
ด้านการศึกษา
DEBUG: Education details count = " . count($educationDetails) . "
"; echo "DEBUG: Education details = " . print_r($educationDetails, true) . "
"; echo "DEBUG: !empty check = " . (!empty($educationDetails) ? 'true' : 'false') . "
"; } if (is_array($educationDetails) && count($educationDetails) > 0) { $count = 1; foreach($educationDetails as $detail): if (isset($detail['issue'])) { $displayText = $detail['issue']; if (!empty($detail['other_detail']) && $detail['other_detail'] != '') { $displayText .= ' (' . $detail['other_detail'] . ')'; } ?>
.
ไม่มีข้อมูลด้านการศึกษา 0
ด้านอาชีพ
DEBUG: Career details count = " . count($careerDetails) . "
"; echo "DEBUG: Career details = " . print_r($careerDetails, true) . "
"; echo "DEBUG: !empty check = " . (!empty($careerDetails) ? 'true' : 'false') . "
"; } if (is_array($careerDetails) && count($careerDetails) > 0) { $count = 1; foreach($careerDetails as $detail): if (isset($detail['issue'])) { $displayText = $detail['issue']; if (!empty($detail['other_detail']) && $detail['other_detail'] != '') { $displayText .= ' (' . $detail['other_detail'] . ')'; } ?>
.
ไม่มีข้อมูลด้านอาชีพ 0
ด้านส่วนตัว
DEBUG: Personal details count = " . count($personalDetails) . "
"; echo "DEBUG: Personal details = " . print_r($personalDetails, true) . "
"; echo "DEBUG: !empty check = " . (!empty($personalDetails) ? 'true' : 'false') . "
"; } if (is_array($personalDetails) && count($personalDetails) > 0) { $count = 1; foreach($personalDetails as $detail): if (isset($detail['issue'])) { $displayText = $detail['issue']; if (!empty($detail['other_detail']) && $detail['other_detail'] != '') { $displayText .= ' (' . $detail['other_detail'] . ')'; } ?>
.
ไม่มีข้อมูลด้านส่วนตัว 0
การให้ความช่วยเหลือ
DEBUG: Referral services count = " . count($referralServices) . "
"; echo "DEBUG: Referral services = " . print_r($referralServices, true) . "
"; echo "DEBUG: !empty check = " . (!empty($referralServices) ? 'true' : 'false') . "
"; } if (is_array($referralServices) && count($referralServices) > 0) { $count = 1; foreach($referralServices as $service): if (isset($service['help_title'])) { $displayText = $service['help_title']; // ถ้าเป็นเลข 6 และมี sub_help_title ให้แสดงด้วย if (isset($service['referral_services']) && $service['referral_services'] == 6 && !empty($service['sub_help_title'])) { $displayText .= ' - ' . $service['sub_help_title']; } ?>
.
ไม่มีข้อมูลการให้ความช่วยเหลือ 0