<?php
include "Admin/config/setup.php";
   $event_id = $_GET['event'] ;
   $sno = 1;
   $sql = "SELECT * FROM apply_request WHERE event_id = '$event_id' " ;
   $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','E.Date','Name','Mobile Number','Category','FIDE ID','AICF ID','TNSCA ID','CHESS ID','LICHESS ID','Entry Fee','A.Date'); 
    fputcsv($file, $fields); 
     
    // Output each row of the data, format line as csv and write to file pointer 
    $i=1;
    while($row = $query->fetch_assoc()){ 
      // print_r($row);exit;

        // $status = ($row['status'] == 1)?'Active':'Inactive'; 
        $user = $row['user_id'];
        $player_id = $row['player_id'];
        $event_sql = $config->query("SELECT * FROM event_post WHERE event_id = '$event_id' AND e_status = 2 ");
        $event = $event_sql->fetch_assoc();
        // $cate_id = $event['sub_cate_id'];
        //   $sub_sql = "SELECT * FROM sub_category WHERE Sub_Category_Id = '$cate_id' AND Sub_Category_status = 1 " ;
        //   $sub_res = $config->query($sub_sql);
        //   $sub = $sub_res->fetch_assoc();
           $ply_sql = $config->query("SELECT * FROM players WHERE id = '$player_id' AND user_id = '$user' " );
           $ply_res = $ply_sql->fetch_assoc();
           // print_r($row);exit;
           $eventData=date('d-m-Y',strtotime($event['e_date']));
           $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'] : ' - ' ;
           $entry=$row['entry'] != 'free' ? $row['entry_fees'] - $row['comm_amt'] : 'Free' ;
           $apply_on=date('d-m-Y h:i a',strtotime($row['apply_on']));
        $lineData = array($i,$eventData,$ply_res['name'], $ply_res['phone'], $row['entry_type'],$fide_id,$aicf_id,$tnsca_id, $chess_id, $lichess_id, $entry ,$apply_on ); 
        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; 
 ?>