<?php

$userAssocArray = $sql->assoc_array("
    SELECT
        uk_name AS 'Név',
        GROUP_CONCAT(trt_name) as 'Szint',
        sc_name as 'Iskola',
        uk_email as 'Email',
        uk_phone as 'Telefonszám',
        p1.up_name as 'Szülő1',
        p1.up_email as 'Sz1_Email',
        p1.up_phone as 'Sz1_Telefonszám',
        p2.up_name as 'Szülő2',
        p2.up_email as 'Sz2_Email',
        p2.up_phone as 'Sz2_Telefonszám'
    FROM
        user_kid
            LEFT JOIN
        user_parent p1 ON p1.up_id = uk_parent_1
            LEFT JOIN
        user_parent p2 ON p2.up_id = uk_parent_2
            LEFT JOIN
        school ON sc_id = uk_school_sc_id
            LEFT JOIN
        user_kid_training_type ON uktt_user_kid_uk_id = uk_id
            LEFT JOIN
        training_type ON trt_id = uktt_training_type_tt_id
    WHERE
        uk_deleted = 0 AND uk_is_active = 1
    GROUP BY uk_name
    ORDER BY uk_name;
");

$filename = "taglista.csv";
$fp = fopen('php://output', 'w');

fputcsv($fp, array_keys(reset($userAssocArray)), ';');
ob_end_clean();

header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);

foreach($userAssocArray as $row) {
    fputcsv($fp, $row, ';');
}
exit(1);
