isLoggedIn()) { die('Unauthorized access'); } $db = Database::getInstance(); // Set headers for CSV download header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename="attributes_' . date('Y-m-d') . '.csv"'); // Create output stream $output = fopen('php://output', 'w'); // Add BOM for Excel UTF-8 compatibility fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF)); // Write headers $headers = ['ID', 'Attribute Name', 'Choice Type', 'Choices', 'Created By', 'Created Date']; fputcsv($output, $headers); // Get all attributes with creator info $query = $db->query(" SELECT a.*, u.full_name as creator_name, DATE_FORMAT(a.created_at, '%Y-%m-%d %H:%i:%S') as formatted_date FROM attributes a LEFT JOIN users u ON a.created_by = u.id ORDER BY a.created_at ASC "); // Write data rows while ($attr = $query->fetch_assoc()) { $choices = json_decode($attr['choices'], true); $choicesText = implode(', ', $choices); $row = [ $attr['id'], $attr['name'], $attr['choice_type'], $choicesText, $attr['creator_name'], $attr['formatted_date'] ]; fputcsv($output, $row); } fclose($output); exit;