<?php 
/*
if (isset($_SERVER['REMOTE_ADDR'])) {
    die(':)');
}
*/
$submit = 1;
if($submit = 1) {
echo "Cron is running";

$hostname   = 'localhost';
$username   = 'supportf_user';
$password   = 'cO?.dTHbAXo{';
$myDatabase = 'supportf_db';

try {
    $conn = new PDO('mysql:host=localhost;dbname='.$myDatabase, $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    

    $stmt = $conn->prepare("SELECT apl_repair_list.repair_id AS ri, apl_repair_list.item_name AS ni, apl_repair_list.item_issue AS zi, apl_repair_list.repair_sent_date AS fi
			FROM apl_repair_list
			INNER JOIN apl_station
			ON apl_repair_list.originating_station = apl_station.station_name");

    $stmt->execute();

	$filelocation = 'csvfiles/';
	$filename     = 'export-'.date('Y-m-d H.i.s').'.csv';
	$file_export  =  $filelocation . $filename;

    $data = fopen($file_export, 'w');

    $csv_fields = array();

	$csv_fields[] = 'repair_id';
	$csv_fields[] = 'item_name';
	$csv_fields[] = 'item_issue';
	$csv_fields[] = 'repair_sent_date';

	fputcsv($data, $csv_fields);

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        fputcsv($data, $row);
    }

} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

}