<?php
header('Content-Type: application/json; charset=utf-8');
require __DIR__ . "/db.php";

$divisions = [
  "leader","singer","keyboard","guitar","bass","drum",
  "manager","lighting","multimedia","kamera"
];

$data = [];
foreach ($divisions as $d) {
    $data[$d] = [];
}

$sql = "SELECT division, name FROM division_names ORDER BY division, name";
$res = $conn->query($sql);

if ($res) {
    while ($row = $res->fetch_assoc()) {
        $div  = $row['division'];
        $name = $row['name'];
        if (isset($data[$div])) {
            $data[$div][] = $name;
        }
    }

    echo json_encode([
        "success" => true,
        "data" => $data
    ]);

} else {
    echo json_encode([
        "success" => false,
        "message" => $conn->error
    ]);
}