# CSV Download Feature Documentation

## Overview
This feature allows users to download student data in CSV format with filtering capabilities by session, class, and group.

## Files Created/Modified

### 1. `csv_download.php`
- Main HTML page with filtering options
- Allows filtering by session, class, and group
- Shows available fields in the CSV export

### 2. `download_csv.php`
- Backend script that handles the CSV generation
- Implements filtering based on GET parameters
- Joins students with classes and subjects tables
- Outputs properly formatted CSV data

### 3. Modified `index.php`
- Added link to CSV download in the quick actions section

## Features

### Filtering Options
- **Session**: Filter by academic session (e.g., 2023, 2024, 2025)
- **Class**: Filter by class name (e.g., One, Two, Three)
- **Group**: Filter by student group (e.g., Science, Commerce, Arts, General)

### CSV Fields Exported
The following fields are included in the CSV export:

1. `first_name` - Student's first name
2. `last_name` - Student's last name
3. `father_name` - Student's father's name
4. `mother_name` - Student's mother's name
5. `email` - Student's email address
6. `phone` - Student's phone number
7. `address` - Student's address
8. `date_of_birth` - Student's date of birth
9. `gender` - Student's gender (male/female/other)
10. `class_name` - Name of the class the student belongs to
11. `session` - Academic session
12. `group_name` - Student's group (Science, Commerce, Arts, etc.)
13. `roll_number` - Student's roll number
14. `admission_date` - Date of admission
15. `guardian_name` - Name of the student's guardian
16. `guardian_phone` - Guardian's phone number
17. `guardian_email` - Guardian's email address
18. `guardian_address` - Guardian's address
19. `subject_codes` - Comma-separated list of subject codes the student is enrolled in

## Database Tables Used
- `students` - Main student information
- `classes` - Class information (joined to get class name)
- `student_subjects` - Junction table linking students to subjects
- `subjects` - Subject information (joined to get subject codes)

## Implementation Details
- Uses PDO prepared statements to prevent SQL injection
- Implements proper CSV escaping for fields containing commas or special characters
- Groups subjects by student using GROUP_CONCAT
- Orders results by roll number for better organization

## Usage
1. Navigate to the CSV download page from the main dashboard
2. Select the desired filters (session, class, group)
3. Click "Download CSV File" to generate and download the filtered data
4. The CSV file will be downloaded with the name "student_data.csv"

## Security
- Uses parameterized queries to prevent SQL injection
- Validates user input through the filtering parameters
- Properly sanitizes output for CSV format

## Notes
- The subject_codes field is generated by concatenating all subject codes for each student
- If a student has no subjects assigned, the subject_codes field will be empty
- The download will only include active students (status = 'active')