<?php 
// Load the database connection
include 'dbc.php';


//CSV Filename
$fnames = 'author_list.csv';

// Header Row Data: Array
$header = ["ID","Student Name", "Father Name", "Class Name", "Section", "Cell"];

// Selecting all authors query
//$qry = $conn->query("SELECT * FROM `admission` order by `id` asc");
$sql = "SELECT * FROM admission";
$result1=$conn->query($sql);
if($result1->num_rows <= 0){
    echo "<script> alert('No data has fetched.'); location.href = './'; </script>";
    exit;
}

//Open a File
$file = fopen("php://memory","w");

// Attach Header
fputcsv($file, $header,',');


// Loop the authors and put it into the CSV file
 
while($row = $result1->fetch_assoc()){
    fputcsv($file, [$row['std_rollno'], $row['std_name'], $row['fname'], $row['class_name'], $row['cls_sec'], $row['cell_1'],',');
}

fseek($file,0);

// Add headers to download the file
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="'.$fnames.'";');

// Read File 
fpassthru($file);
exit;