26 มี.ค. 69) // --------------------------------------------------------- function ThaiDateShort($dateStr) { if (empty($dateStr) || $dateStr == '0000-00-00' || $dateStr == '-') return '-'; // ดึงเฉพาะส่วนวันที่ (ทิ้งเวลา) $d = explode("-", explode(" ", $dateStr)[0]); if (count($d) != 3) return '-'; $year = intval($d[0]); // ถ้าเป็น ค.ศ. ให้บวก 543 เป็น พ.ศ. if ($year < 2400) { $year += 543; } $year_short = substr((string)$year, -2); // เอาแค่ 2 ตัวท้าย $month = intval($d[1]); $day = intval($d[2]); $th_months = array("", "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค."); return $day . " " . $th_months[$month] . " " . $year_short; } // --------------------------------------------------------- // 2. รับค่าสิทธิ์การใช้งาน (RBAC) เพื่อกรองข้อมูลไม่ให้ข้ามเขต // --------------------------------------------------------- $raw_school_id = isset($schoolID) ? $schoolID : (isset($_SESSION['schoolID']) ? $_SESSION['schoolID'] : ''); $raw_school_id = preg_replace('/[^a-zA-Z0-9_]/', '', $raw_school_id); $current_user_type = isset($user_type_id) ? intval($user_type_id) : (isset($_SESSION['user_type_id']) ? intval($_SESSION['user_type_id']) : 0); $is_admin = ($current_user_type === 9 || empty($raw_school_id) || strtolower($raw_school_id) === 'admin'); if ($is_admin) { $user_school_id = ''; } else { $user_school_id = $raw_school_id; } // --------------------------------------------------------- // 3. รับค่าจากตัวกรองหน้า Dashboard (ที่ส่งมาทาง URL) // --------------------------------------------------------- $start_date = isset($_GET['start_date']) ? $_GET['start_date'] : ''; $end_date = isset($_GET['end_date']) ? $_GET['end_date'] : ''; $service_type = isset($_GET['service_type']) ? $_GET['service_type'] : ''; $search_term = isset($_GET['search_term']) ? trim($_GET['search_term']) : ''; // --------------------------------------------------------- // 4. สร้างเงื่อนไขการค้นหา (WHERE Clause) // --------------------------------------------------------- $where_conditions = array(); $params = array(); // กรองตามสิทธิ์การเข้าถึง if ($user_school_id !== '' && $user_school_id !== '0') { if ($current_user_type === 1) { $where_conditions[] = "g.schoolID LIKE ?"; $params[] = '%' . substr($user_school_id, 0, 4) . '%'; } else if ($current_user_type === 2) { $where_conditions[] = "g.schoolID LIKE ?"; $params[] = '%' . substr($user_school_id, 0, 6) . '%'; } else { $where_conditions[] = "g.schoolID LIKE ?"; $params[] = '%' . $user_school_id . '%'; } } // กรองตามวันที่ if (!empty($start_date)) { $where_conditions[] = "g.created_at >= ?"; $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); } // --------------------------------------------------------- // 5. ดึงข้อมูลจากฐานข้อมูลตามเงื่อนไข // --------------------------------------------------------- $stmt = $pdo->prepare(" SELECT g.id, g.created_at, g.participant_count, g.participant_type, g.objectives, g.objectives_activities, g.guidance_type, g.activity_method, g.goal_achieved, g.service_details, s.province, s.amphur FROM group_guidance AS g LEFT JOIN school AS s ON g.schoolID = s.schoolID WHERE 1=1 {$where_clause} ORDER BY g.created_at DESC; "); $stmt->execute($params); $users = $stmt->fetchAll(PDO::FETCH_ASSOC); // --------------------------------------------------------- // 6. ตั้งค่า Header เพื่อ Export เป็นไฟล์ Excel // --------------------------------------------------------- $filename = "report_group_guidance_" . date('Y-m-d') . ".xls"; header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=\"$filename\""); header("Pragma: no-cache"); header("Expires: 0"); // บังคับให้รองรับภาษาไทยใน Excel (สำคัญมาก) echo "\xEF\xBB\xBF"; ?> 0) { foreach ($users as $row) { // ดึงวันที่จากฟิลด์ created_at มาแปลง $service_date_th = ThaiDateShort($row['created_at']); // บวกยอดรวมในแต่ละรอบของลูป $total_participants += $row['participant_count']; ?> "; } ?>
ลำดับที่ วันที่รับบริการ จังหวัด อำเภอ จำนวนผู้เข้าร่วมกิจกรรม ประเภทผู้เข้าร่วมกิจกรรม ชื่อกิจกรรม วัตถุประสงค์ ประเภทของการแนะแนว วิธีการจัดกิจกรรมสัมมนา บทสรุป บันทึกผลการจัดกิจกรรม
สกร. ผู้เรียนที่จบไป
ไม่พบข้อมูลตามเงื่อนไขที่ค้นหา
รวมทั้งสิ้น คน