<?php
include "Admin/config/setup.php";
   $user_id = $_GET['user'] ;
   $sno = 1;
 $sql = "SELECT * FROM players WHERE user_id = '$user_id' " ; 
 $res = $config->query($sql);
   $query = $config->query($sql);

   // $res->fetch_assoc();
 if($query->num_rows > 0){ 
    $delimiter = ","; 
    $filename = "players-data_" . date('Y-m-d') . ".csv"; 
    header("Content-Description: File Transfer"); 
    header('Content-Type: text/csv'); 
    header('Content-Disposition: attachment; filename="' . $filename . '";'); 
    // Create a file pointer 
	$file = fopen('php://output', 'w');
     
    // Set column headers 
    $fields = array('S.No','Name','Name Initial','School Name','Father Name','DOB','Gender','Country','State','City','Address','Email','Mobile Number','FIDE ID','AICF ID','TNSCA ID','CHESS ID','LICHESS ID','C.Date'); 
    fputcsv($file, $fields); 
     
    // Output each row of the data, format line as csv and write to file pointer 
    $i=1;
    while($ply_res = $query->fetch_assoc()){ 
           // print_r($row);exit;
           $dob=date('d-m-Y',strtotime($ply_res['dob']));
           $fide_id= $ply_res['fide_id'] != '' ? $ply_res['fide_id'] : ' - ' ;
           $aicf_id=$ply_res['aicf_id'] != '' ? $ply_res['aicf_id'] : ' - ' ;
           $tnsca_id=$ply_res['tnsca_id'] != '' ? $ply_res['tnsca_id'] : ' - ' ;
           $chess_id=$ply_res['chess_id'] != '' ? $ply_res['chess_id'] : ' - ' ;
           $lichess_id=$ply_res['lichess_id'] != '' ? $ply_res['lichess_id'] : ' - ' ;
           $created_at=date('d-m-Y',strtotime($ply_res['created_at']));
        $lineData = array($i,$ply_res['name'],$ply_res['name_initial'], $ply_res['school_name'],$ply_res['f_name'] ,$dob ,$ply_res['gender'],$ply_res['country'],$ply_res['state'],$ply_res['city'],$ply_res['address'],$ply_res['email'],$ply_res['phone'],$fide_id,$aicf_id,$tnsca_id, $chess_id, $lichess_id ,$created_at ); 
        fputcsv($file, $lineData); 
    $i++; } 
      fclose($file); 
    // Move back to beginning of file 
    // fseek($f, 0); 
     
    // Set headers to download file rather than displayed 
    // header('Content-Type: text/csv'); 
    // header('Content-Disposition: attachment; filename="' . $filename . '";'); 
     
    //output all remaining data on a file pointer 
    // fpassthru($f); 
} 
exit; 
 ?>