= ?"; $params[] = $start_date . ' 00:00:00'; } if (!empty($end_date)) { $where_conditions[] = "g.created_at <= ?"; $params[] = $end_date . ' 23:59:59'; } if (!empty($service_type)) { $where_conditions[] = "g.guidance_type = ?"; $params[] = $service_type; } if (!empty($search_term)) { $where_conditions[] = "(s.firstname LIKE ? OR s.province LIKE ? OR s.amphur LIKE ?)"; $search_param = '%' . $search_term . '%'; $params[] = $search_param; $params[] = $search_param; $params[] = $search_param; } $where_clause = ''; if (!empty($where_conditions)) { $where_clause = ' AND ' . implode(' AND ', $where_conditions); } $filter_params = $params; $db_error_message = ''; try { if (!isset($pdo) || !$pdo) { throw new PDOException("ไม่สามารถเชื่อมต่อฐานข้อมูลได้"); } $has_school_table = false; try { $check_school = $pdo->query("SHOW TABLES LIKE 'school'"); $has_school_table = ($check_school && $check_school->rowCount() > 0); } catch (Exception $e) { $has_school_table = false; } // ดึงชื่อประเภทจาก typeActivity $stmt_ta = $pdo->query("SELECT id, title FROM typeActivity ORDER BY id ASC"); $ta_rows = $stmt_ta->fetchAll(PDO::FETCH_ASSOC); $ta_titles = array(1 => 'ตลอดชีวิต', 2 => 'พัฒนาตนเอง', 3 => 'คุณวุฒิฯ', 4 => 'ประชาชน'); if ($ta_rows) { foreach ($ta_rows as $r) { $ta_titles[$r['id']] = $r['title']; } } if ($has_school_table) { $sql = "SELECT COUNT(g.id) as total_events, SUM(g.participant_count) as total_participants FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE 1=1" . $where_clause; } else { $sql = "SELECT COUNT(g.id) as total_events, SUM(g.participant_count) as total_participants FROM group_guidance g WHERE 1=1" . str_replace(array("s.firstname", "s.province", "s.amphur"), array("''", "''", "''"), $where_clause); } $stmt = $pdo->prepare($sql); $stmt->execute($filter_params); $result = $stmt->fetch(PDO::FETCH_ASSOC); $totalEvents = $result ? (empty($result['total_events']) ? 0 : $result['total_events']) : 0; $totalParticipants = $result ? (empty($result['total_participants']) ? 0 : $result['total_participants']) : 0; if ($has_school_table) { $sql = "SELECT t.title as type_name, SUM(g.participant_count) as count FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID LEFT JOIN typeActivity t ON g.participant_type = t.id WHERE g.participant_type IS NOT NULL" . $where_clause . " GROUP BY g.participant_type, t.title"; } else { $sql = "SELECT t.title as type_name, SUM(g.participant_count) as count FROM group_guidance g LEFT JOIN typeActivity t ON g.participant_type = t.id WHERE g.participant_type IS NOT NULL" . str_replace(array("s.firstname", "s.province", "s.amphur"), array("''", "''", "''"), $where_clause) . " GROUP BY g.participant_type, t.title"; } $stmt = $pdo->prepare($sql); $stmt->execute($filter_params); $participantTypes = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!$participantTypes) { $participantTypes = array(); } // Chart 1: รายกลุ่ม (นับครั้ง) $sql_group = "SELECT g.guidance_type, COUNT(g.id) as tot, SUM(CASE WHEN g.participant_type = '1' THEN 1 ELSE 0 END) as pt1, SUM(CASE WHEN g.participant_type = '2' THEN 1 ELSE 0 END) as pt2, SUM(CASE WHEN g.participant_type = '3' THEN 1 ELSE 0 END) as pt3, SUM(CASE WHEN g.participant_type = '4' THEN 1 ELSE 0 END) as pt4 FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE g.guidance_type IN ('1','2','3') " . $where_clause . " GROUP BY g.guidance_type"; $stmt = $pdo->prepare($sql_group); $stmt->execute($filter_params); $groupOverall = $stmt->fetchAll(PDO::FETCH_ASSOC); $go_data = array(1 => ['tot' => 0, 'pt1' => 0, 'pt2' => 0, 'pt3' => 0, 'pt4' => 0], 2 => ['tot' => 0, 'pt1' => 0, 'pt2' => 0, 'pt3' => 0, 'pt4' => 0], 3 => ['tot' => 0, 'pt1' => 0, 'pt2' => 0, 'pt3' => 0, 'pt4' => 0]); foreach ($groupOverall as $r) { $gtype = $r['guidance_type']; if (isset($go_data[$gtype])) { $go_data[$gtype] = ['tot' => intval($r['tot']), 'pt1' => intval($r['pt1']), 'pt2' => intval($r['pt2']), 'pt3' => intval($r['pt3']), 'pt4' => intval($r['pt4'])]; } } // Chart 2: รายคน (นับคน) $sql_indiv = "SELECT g.guidance_type, SUM(g.participant_count) as tot, SUM(CASE WHEN g.participant_type = '1' THEN g.participant_count ELSE 0 END) as pt1, SUM(CASE WHEN g.participant_type = '2' THEN g.participant_count ELSE 0 END) as pt2, SUM(CASE WHEN g.participant_type = '3' THEN g.participant_count ELSE 0 END) as pt3, SUM(CASE WHEN g.participant_type = '4' THEN g.participant_count ELSE 0 END) as pt4 FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE g.guidance_type IN ('1','2','3') " . $where_clause . " GROUP BY g.guidance_type"; $stmt = $pdo->prepare($sql_indiv); $stmt->execute($filter_params); $detailOverall = $stmt->fetchAll(PDO::FETCH_ASSOC); $do_data = array(1 => ['tot' => 0, 'pt1' => 0, 'pt2' => 0, 'pt3' => 0, 'pt4' => 0], 2 => ['tot' => 0, 'pt1' => 0, 'pt2' => 0, 'pt3' => 0, 'pt4' => 0], 3 => ['tot' => 0, 'pt1' => 0, 'pt2' => 0, 'pt3' => 0, 'pt4' => 0]); foreach ($detailOverall as $r) { $gtype = $r['guidance_type']; if (isset($do_data[$gtype])) { $do_data[$gtype] = ['tot' => intval($r['tot']), 'pt1' => intval($r['pt1']), 'pt2' => intval($r['pt2']), 'pt3' => intval($r['pt3']), 'pt4' => intval($r['pt4'])]; } } // 🌟 อัตราความสำเร็จ (จัดเรียงและนับยอดด้วย success_count แทน rate) 🌟 $sql = "SELECT CASE WHEN g.guidance_type = '1' THEN 'ด้านการศึกษา' WHEN g.guidance_type = '2' THEN 'ด้านอาชีพ' WHEN g.guidance_type = '3' THEN 'ด้านส่วนตัวและสังคม' ELSE 'อื่นๆ' END as guidance_name, SUM(CASE WHEN g.goal_achieved = '1' THEN g.participant_count ELSE 0 END) as success_count, SUM(g.participant_count) as total_count FROM group_guidance g INNER JOIN (SELECT schoolID, guidance_type, MAX(id) as max_id FROM group_guidance GROUP BY schoolID, guidance_type) latest ON g.id = latest.max_id LEFT JOIN school s ON g.schoolID = s.schoolID WHERE g.participant_count IS NOT NULL AND g.guidance_type IS NOT NULL" . $where_clause . " GROUP BY g.guidance_type ORDER BY success_count DESC"; $stmt = $pdo->prepare($sql); $stmt->execute($filter_params); $successRates = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!$successRates) { $successRates = array(); } // TOP 10 if ($has_school_table) { $sql = "SELECT IFNULL({$group_col}, 'ไม่ระบุ') as item_name, COUNT(*) as total_count FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE g.schoolID IS NOT NULL" . $where_clause . " GROUP BY item_name HAVING total_count > 0 ORDER BY total_count DESC LIMIT 10"; } else { $sql = "SELECT 'กรุงเทพมหานคร' as item_name, COUNT(*) as total_count FROM group_guidance g WHERE 1=1" . str_replace(array("s.firstname", "s.province", "s.amphur"), array("''", "'กรุงเทพมหานคร'", "''"), $where_clause) . " HAVING total_count > 0 LIMIT 10"; } $stmt = $pdo->prepare($sql); $stmt->execute($filter_params); $topItems = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!$topItems) { $topItems = array(); } $dynamicGuidance = array(); $dynamicDetail = array(); $dynamicSuccess = array(); foreach ($topItems as $item) { $iname = $item['item_name']; $item_params = array($iname); if (!empty($filter_params)) { foreach ($filter_params as $param) { $item_params[] = $param; } } if ($has_school_table) { // Dynamic Chart 1 $stmt = $pdo->prepare("SELECT g.guidance_type, COUNT(g.id) as tot, SUM(CASE WHEN g.participant_type = '1' THEN 1 ELSE 0 END) as pt1, SUM(CASE WHEN g.participant_type = '2' THEN 1 ELSE 0 END) as pt2, SUM(CASE WHEN g.participant_type = '3' THEN 1 ELSE 0 END) as pt3, SUM(CASE WHEN g.participant_type = '4' THEN 1 ELSE 0 END) as pt4 FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE IFNULL({$group_col}, 'ไม่ระบุ') = ? AND g.guidance_type IN ('1','2','3') " . $where_clause . " GROUP BY g.guidance_type"); $stmt->execute($item_params); $g_res = $stmt->fetchAll(PDO::FETCH_ASSOC); $d_go = array(1 => ['tot' => 0, 'pt1' => 0, 'pt2' => 0, 'pt3' => 0, 'pt4' => 0], 2 => ['tot' => 0, 'pt1' => 0, 'pt2' => 0, 'pt3' => 0, 'pt4' => 0], 3 => ['tot' => 0, 'pt1' => 0, 'pt2' => 0, 'pt3' => 0, 'pt4' => 0]); foreach ($g_res as $r) { $gtype = $r['guidance_type']; if (isset($d_go[$gtype])) { $d_go[$gtype] = ['tot' => intval($r['tot']), 'pt1' => intval($r['pt1']), 'pt2' => intval($r['pt2']), 'pt3' => intval($r['pt3']), 'pt4' => intval($r['pt4'])]; } } $dynamicGuidance[$iname] = array( 'labels' => ['ด้านการศึกษา', 'ด้านอาชีพ', 'ด้านส่วนตัวและสังคม'], 'data' => [$d_go[1]['tot'], $d_go[2]['tot'], $d_go[3]['tot']], 'pt1' => [$d_go[1]['pt1'], $d_go[2]['pt1'], $d_go[3]['pt1']], 'pt2' => [$d_go[1]['pt2'], $d_go[2]['pt2'], $d_go[3]['pt2']], 'pt3' => [$d_go[1]['pt3'], $d_go[2]['pt3'], $d_go[3]['pt3']], 'pt4' => [$d_go[1]['pt4'], $d_go[2]['pt4'], $d_go[3]['pt4']] ); // Dynamic Chart 2 $stmt = $pdo->prepare("SELECT g.guidance_type, SUM(g.participant_count) as tot, SUM(CASE WHEN g.participant_type = '1' THEN g.participant_count ELSE 0 END) as pt1, SUM(CASE WHEN g.participant_type = '2' THEN g.participant_count ELSE 0 END) as pt2, SUM(CASE WHEN g.participant_type = '3' THEN g.participant_count ELSE 0 END) as pt3, SUM(CASE WHEN g.participant_type = '4' THEN g.participant_count ELSE 0 END) as pt4 FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE IFNULL({$group_col}, 'ไม่ระบุ') = ? AND g.guidance_type IN ('1','2','3') " . $where_clause . " GROUP BY g.guidance_type"); $stmt->execute($item_params); $i_res = $stmt->fetchAll(PDO::FETCH_ASSOC); $d_do = array(1 => ['tot' => 0, 'pt1' => 0, 'pt2' => 0, 'pt3' => 0, 'pt4' => 0], 2 => ['tot' => 0, 'pt1' => 0, 'pt2' => 0, 'pt3' => 0, 'pt4' => 0], 3 => ['tot' => 0, 'pt1' => 0, 'pt2' => 0, 'pt3' => 0, 'pt4' => 0]); foreach ($i_res as $r) { $gtype = $r['guidance_type']; if (isset($d_do[$gtype])) { $d_do[$gtype] = ['tot' => intval($r['tot']), 'pt1' => intval($r['pt1']), 'pt2' => intval($r['pt2']), 'pt3' => intval($r['pt3']), 'pt4' => intval($r['pt4'])]; } } $dynamicDetail[$iname] = array( 'labels' => ['ด้านการศึกษา', 'ด้านอาชีพ', 'ด้านส่วนตัวและสังคม'], 'data' => [$d_do[1]['tot'], $d_do[2]['tot'], $d_do[3]['tot']], 'pt1' => [$d_do[1]['pt1'], $d_do[2]['pt1'], $d_do[3]['pt1']], 'pt2' => [$d_do[1]['pt2'], $d_do[2]['pt2'], $d_do[3]['pt2']], 'pt3' => [$d_do[1]['pt3'], $d_do[2]['pt3'], $d_do[3]['pt3']], 'pt4' => [$d_do[1]['pt4'], $d_do[2]['pt4'], $d_do[3]['pt4']] ); // 🌟 Dynamic Chart 3 (ดึงยอดคน success_count โดยตรง) 🌟 $stmt = $pdo->prepare("SELECT CASE WHEN g.guidance_type = '1' THEN 'ด้านการศึกษา' WHEN g.guidance_type = '2' THEN 'ด้านอาชีพ' WHEN g.guidance_type = '3' THEN 'ด้านส่วนตัวและสังคม' ELSE 'อื่นๆ' END as guidance_name, SUM(CASE WHEN g.goal_achieved = '1' THEN g.participant_count ELSE 0 END) as success_count, SUM(g.participant_count) as total_count FROM group_guidance g INNER JOIN (SELECT schoolID, guidance_type, MAX(id) as max_id FROM group_guidance GROUP BY schoolID, guidance_type) latest ON g.id = latest.max_id LEFT JOIN school s ON g.schoolID = s.schoolID WHERE IFNULL({$group_col}, 'ไม่ระบุ') = ? AND g.participant_count IS NOT NULL AND g.guidance_type IS NOT NULL" . $where_clause . " GROUP BY g.guidance_type ORDER BY success_count DESC"); $stmt->execute($item_params); $s_res = $stmt->fetchAll(PDO::FETCH_ASSOC); $s_labels = array(); $s_success = array(); $s_total = array(); foreach ($s_res as $r) { $s_labels[] = $r['guidance_name']; $s_success[] = intval($r['success_count']); $s_total[] = intval($r['total_count']); } $dynamicSuccess[$iname] = array( 'labels' => $s_labels ? $s_labels : array('ไม่มีข้อมูล'), 'data' => $s_success ? $s_success : array(0), // ส่งยอด success เข้าไปวาดกราฟแท่ง 'success_count' => $s_success ? $s_success : array(0), 'total_count' => $s_total ? $s_total : array(0) ); } else { $dynamicGuidance[$iname] = array('labels' => array('ไม่มีข้อมูล'), 'data' => array(0), 'pt1' => array(0), 'pt2' => array(0), 'pt3' => array(0), 'pt4' => array(0)); $dynamicDetail[$iname] = array('labels' => array('ไม่มีข้อมูล'), 'data' => array(0), 'pt1' => array(0), 'pt2' => array(0), 'pt3' => array(0), 'pt4' => array(0)); $dynamicSuccess[$iname] = array('labels' => array('ไม่มีข้อมูล'), 'data' => array(0), 'success_count' => array(0), 'total_count' => array(0)); } } } catch (PDOException $e) { $db_error_message = $e->getMessage(); } ?> Dashboard แบบรายกลุ่ม
🚨 พบข้อผิดพลาด (SQL Error):
สถานะการใช้งาน:

Dashboard แบบรายกลุ่ม

จำนวนครั้งที่จัด
กิจกรรมทั้งหมด
จำนวนผู้เข้าร่วม
กิจกรรมทั้งหมด
$c, 'count' => 0, 'label' => $label); $idx++; } } else { $participantTypesArray = array( 'การเรียนรู้ตลอดชีวิต' => array('class' => 'lifelong', 'count' => 0, 'label' => 'การเรียนรู้ตลอดชีวิต'), 'การเรียนรู้เพื่อการพัฒนาตนเอง' => array('class' => 'development', 'count' => 0, 'label' => 'การเรียนรู้เพื่อการพัฒนาตนเอง'), 'การเรียนรู้เพื่อคุณวุฒิตามระดับ' => array('class' => 'qualification', 'count' => 0, 'label' => 'การเรียนรู้เพื่อคุณวุฒิตามระดับ'), 'ประชาชน' => array('class' => 'public', 'count' => 0, 'label' => 'ประชาชน (ทั่วไป)') ); } foreach ($participantTypes as $type) { $tname = !empty($type['type_name']) ? $type['type_name'] : 'ไม่ระบุ'; if (isset($participantTypesArray[$tname])) { $participantTypesArray[$tname]['count'] = intval($type['count']); } else { $participantTypesArray[$tname] = array('class' => 'public', 'count' => intval($type['count']), 'label' => $tname); } } foreach ($participantTypesArray as $data): ?>
TOP 10 ที่ให้บริการแนะแนวมากที่สุด (คลิกที่แท่งเพื่อดูรายละเอียด)
ประเภทการแนะแนว (จำนวนครั้ง) แบ่งเป็นรายด้าน
ประเภทการแนะแนว (จำนวนผู้เข้าร่วมกิจกรรม) แบ่งเป็นรายด้าน
ความสำเร็จ (จำนวนคน) - ภาพรวม