<?php
include_once('includes/connect_database.php'); 
    include_once('functions.php'); 
        function ExportFile($records) {
    $heading = false;
        if(!empty($records))
          foreach($records as $row) {
            if(!$heading) {
              // display field/column names as a first row
              echo implode("\t", array_keys($row)) . "\n";
              $heading = true;
            }
            echo implode("\t", array_values($row)) . "\n";
          }
        exit;
}

    $function = new functions;
    

        if(isset($_SESSION['keyword']) && $_SESSION['keyword']!=''){    
            // check value of keyword variable
            $keyword = $function->sanitize($_SESSION['keyword']);
            $bind_keyword = "%".$keyword."%";
        }else{
            $keyword = "";
            $bind_keyword = $keyword;
        }
            
        if(empty($keyword)){
            $sql_query = "SELECT id,student_name,
        student_dob,
        mobile,
        email,
        guardian,
        mobile_guardian,
        email_guardian,
        address,
        city,
        ten_board,
        ten_studyyear,
        ten_schoolname,
        ten_marks,
        twelve_studyyear,
        twelve_schoolname,
        twelve_marks,twelve_board,twelve_result,course,mode,looking_for_scholarship,created,remark
            FROM tbl_student
                    ORDER BY id DESC";
        }else{
            $sql_query = "SELECT id,student_name,
        student_dob,
        mobile,
        email,
        guardian,
        mobile_guardian,
        email_guardian,
        address,
        city,
        ten_board,
        ten_studyyear,
        ten_schoolname,
        ten_marks,
        twelve_studyyear,
        twelve_schoolname,
        twelve_marks,twelve_board,twelve_result,course,mode,looking_for_scholarship,created,remark
                    FROM tbl_student
                    WHERE (student_name LIKE ? OR email LIKE ? OR mobile LIKE ? OR city LIKE ? OR address LIKE ? OR email_guardian LIKE ? OR guardian LIKE ?    )
                    ORDER BY id DESC";
        }
        
        
        $stmt = $connect->stmt_init();
        if($stmt->prepare($sql_query)) {    
            // Bind your variables to replace the ?s
            if(!empty($keyword)){
                $stmt->bind_param('sssssss', $bind_keyword,$bind_keyword,$bind_keyword,$bind_keyword,$bind_keyword,$bind_keyword,$bind_keyword);
            }
            // Execute query
            $stmt->execute();
            // store result 
            $stmt->store_result();
            $stmt->bind_result($data['id'], 
                    $data['student_name'], 
                    $data['student_dob'], 
                    $data['mobile'], 
                    $data['email'],
                    $data['guardian'],
                    $data['mobile_guardian'],
                    $data['email_guardian'],
                    $data['address'],
                    $data['city'],
                    $data['ten_board'],
                    $data['ten_studyyear'],
                    $data['ten_schoolname'],
                    $data['ten_marks'],
                    $data['twelve_studyyear'],
                    $data['twelve_schoolname'],
                    $data['twelve_marks'],
                    $data['twelve_board'],
                    $data['twelve_result'],
                    $data['course'],
                    $data['mode'],
                    $data['looking_for_scholarship'],$data['created'],$data['remark']
                    );
          // get total records
            $total_records = $stmt->num_rows;
        }
       $dd=array();
       while ($stmt->fetch()){ 
$dd[]=array('ID'=>"FW201821".str_pad($data['id'],3,'0',STR_PAD_LEFT), 
                    'Student Name'=>$data['student_name'], 
                    'Student DOB'=>$data['student_dob'], 
                    'Mobile'=>$data['mobile'], 
                    'Email'=>$data['email'],
                    'Guardian'=>$data['guardian'],
                    "Mobile - Guardian"=>$data['mobile_guardian'],
                    'Email - Guardian'=>$data['email_guardian'],
                    'Address'=>$data['address'],
                    'City'=>$data['city'],
                    'Ten - Board'=>$data['ten_board'],
                    'Ten- Yead of Study'=>$data['ten_studyyear'],
                    'Ten- School Name '=>$data['ten_schoolname'],
                    'Ten- Marks/Grade'=>$data['ten_marks'],
                    'Twelve Study OF Year'=>$data['twelve_studyyear'],
                    'Twelve School Name'=>$data['twelve_schoolname'],
                    'Twelve - Marks/Grade'=>$data['twelve_marks'],
                    'Twelve Board'=>$data['twelve_board'],
                    'Twelve - Result '=>$data['twelve_result'],
                    'Course'=>$data['course'],
                    'Model'=>$data['mode'],
                    'Scholarship'=>$data['looking_for_scholarship'],'Walk-In Date'=>date("Y-m-d",strtotime($data['created'])),'Remark'=>$data['remark']
                    );
}  
 //while ($stmt_paging->fetch()){ 
$filename = date('Y-m-d'). "_student_list.xls";       
            header("Content-Type: application/vnd.ms-excel");
            header("Content-Disposition: attachment; filename=\"$filename\"");
            ExportFile($dd);

        

    ?>