# School Parent App — API (Slim 4)

## Project Structure

```
school-api/
├── public/
│   ├── index.php        ← Web entry point
│   └── .htaccess        ← Routes all requests to index.php
├── src/
│   ├── Database.php     ← PDO database connection
│   └── Routes/
│       ├── result.php     ← GET /api/result/{rollno}
│       ├── attendance.php ← GET /api/attendance/{rollno}
│       └── fee.php        ← GET /api/fee/{adm_no}
├── .env                 ← Your database credentials (never commit this)
├── .htaccess            ← Root redirect to /public
├── composer.json
└── README.md
```

---

## API Endpoints

| Method | URL | Description |
|--------|-----|-------------|
| GET | `/api/result/{rollno}` | Get student result by roll number |
| GET | `/api/attendance/{rollno}` | Get student attendance by roll number |
| GET | `/api/fee/{adm_no}` | Get student fee records by admission number |

### Example Requests
```
GET http://localhost:8080/api/result/1837
GET http://localhost:8080/api/attendance/2430
GET http://localhost:8080/api/fee/2430
```

### Example Response
```json
{
    "status": "true",
    "message": "Students Details",
    "Students": [
        {
            "std_rollno": "1837",
            "std_name": "Ahmed Ali",
            "term": "First Term",
            ...
        }
    ]
}
```

---

## Setup on Your PC (Local)

### Requirements
- PHP 7.4 or higher
- Composer
- MySQL / XAMPP / Laragon

### Step 1 — Install Composer (if not installed)
Download from https://getcomposer.org/download/

### Step 2 — Install project dependencies
Open terminal/command prompt inside the `school-api` folder:
```bash
composer install
```
This will create the `vendor/` folder automatically.

### Step 3 — Configure your database
Open the `.env` file and fill in your details:
```
DB_HOST=localhost
DB_NAME=your_database_name
DB_USER=root
DB_PASS=
DB_CHARSET=utf8mb4
```
If you are using XAMPP, DB_USER is usually `root` and DB_PASS is empty.

### Step 4 — Run the built-in PHP server
```bash
php -S localhost:8080 -t public
```

### Step 5 — Test in browser or Postman
```
http://localhost:8080/api/result/1837
http://localhost:8080/api/attendance/2430
http://localhost:8080/api/fee/2430
```

---

## Deploy on cPanel

### Step 1 — Run composer locally first
```bash
composer install --no-dev --optimize-autoloader
```

### Step 2 — Upload all files
Upload the entire project folder to your hosting via File Manager or FTP.

### Step 3 — Point your domain to /public folder
In cPanel → Domains → point your domain's document root to `school-api/public`

OR if you upload to `public_html`, the root `.htaccess` will redirect automatically.

### Step 4 — Update .env on server
Edit the `.env` file on server with your cPanel database credentials.

### Step 5 — Test your live URL
```
https://yourdomain.com/api/result/1837
```

---

## Important Notes

1. The original API had a bug — it only returned the LAST student record because it was
   echoing inside the while loop and overwriting the same variable each time.
   This version correctly returns ALL records as an array.

2. Prepared statements are used — no SQL injection risk unlike the original files.

3. Never commit your `.env` file to GitHub. Add it to `.gitignore`.
