<?php 
require_once 'includes/database.php';
require_once 'includes/funcs.php';
if (isset($_GET['insno'])){
    $insno =  $_GET['insno'];	$i =  intval($_GET['i']);
	$db=new MySQLDatabase();
	$db->open_connection();
    $query="SELECT * FROM insrecs WHERE insno = '$insno'"; 
    $sql = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
    if ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){
            if($i===0){ $agp = $row['agpno']; }
			else if($i===1){ $agp = $row['agpno1']; }
			else if($i===2){ $agp = $row['agpno2']; }
			else if($i===3){ $agp = $row['agpno3']; }
            $ts = $row['ts'];
			$idate = substr($row['idate'],8,2)."-".substr($row['idate'],5,2)."-".substr($row['idate'] ,0,4); 
			$regno = $row['regno'];		
			$ph = $row['cname'].' / '.$row['insurer'];
			$insco = getsupplier($row['supp']);	
            		$expdate = substr($row['expdate'],8,2)."-".substr($row['expdate'],5,2)."-".substr($row['expdate'] ,0,4); 
			
			$fp = fopen('data.txt', 'w');
			fwrite($fp, $ph.">".$agp.">".$idate.">".$expdate.">".$regno.">".$insco);
			fclose($fp);
		header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header("Content-Type: application/force-download");
    header('Content-Disposition: attachment; filename="data.txt"');
    // header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize('data.txt'));
    ob_clean();
    flush();
    readfile('data.txt');
    exit;
    }
	$db->close_connection();


}
?>