<? include ("../settings.php"); ?>
<?

        $results = $db->run("select a.*,b.user_id,b.user_login,b.user_name from tbl_principle as a, tbl_abstract_user as b where b.user_id = a.abstract_user_id limit 1");
        $result = $results[0];
        $output = "";
        foreach (array_keys($result) as $key){
            if ($output=="")
                $output = "\"".$key."\"";
            else
                $output .= ",\"".$key."\"";
        }
    	$output .= "\n";
	$file = $_SERVER['DOCUMENT_ROOT']."/ECCE/csv/abstractprinciple.csv";
	$fp = fopen($file, 'w');
	fwrite($fp, $output);
	$results = $db->run("select a.*,b.user_id,b.user_login,b.user_name from tbl_principle as a, tbl_abstract_user as b where b.user_id = a.abstract_user_id");
	foreach ($results as $result){

	        $output = "";
			foreach ($result as $record){
	            if ($output=="")
	                $output = "\"".$record."\"";
	            else
	                $output .= ",\"".$record."\"";
	        }
	        
	        $output .= "\n";
	        fwrite($fp, $output);
	}

	fclose($fp);

	// Set headers
	header("Cache-Control: public");
	header("Content-Description: File Transfer");
	header("Content-Disposition: attachment; filename=abstractprinciple.csv");
	header("Content-Type: application/zip");
	header("Content-Transfer-Encoding: binary");
	
	// Read the file from disk
	readfile($file);

?>