<?php 

include 'check.php';
include '../db.inc.php';
// Load the database configuration file 
//include_once 'dbConfig.php'; 
 

if(isset($_GET['number'])) {
    $number=$_GET['number'];
    $sql="SELECT * FROM history WHERE number='$number' ORDER BY date DESC";
 }
 else {
    $sql="SELECT * FROM history ORDER BY date DESC";
 }
 $query=mysqli_query($connectionString,$sql);
 $count=mysqli_num_rows($query);




$filename = "transaction-history" . date('Y-m-d') . ".csv"; 
$delimiter = ","; 
 
// Create a file pointer 
$f = fopen('php://memory', 'w'); 
 
// Set column headers 
$fields = array('Account ID', 'Tran. ID', 'Date', 'Tran. Type', 'Amount', 'Description','Status'); 
fputcsv($f, $fields, $delimiter); 
 
// Get records from the database  
if($count > 0){ 
    // Output each row of the data, format line as csv and write to file pointer 
    while($get=mysqli_fetch_array($query,MYSQLI_ASSOC)){ 
        $lineData = array($get['number'], $get['tid'], strftime('%d-%m-%Y %I:%M %p',strtotime($get['date'])), $get['type'], number_format($get['amount'],2), stripslashes($get['details']),$get['status']); 
        fputcsv($f, $lineData, $delimiter); 
    } 
} 
 
// 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 from file 
exit();