<?php
//prihlaseni do daatbaze
require 'db.php';

$today = getdate();
//nastaveni data souboru
$datumsouboru = ($today["mday"].'_'.$today["mon"].'_'.$today["year"].'_'.$today["hours"].':'.$today["minutes"]);
print_r("$datumsouboru");
//export dat za 30dni 
if(isset($_POST["export30"])){
    header('Content-Type:text/csv; charset=utf-8');
    header('Content-Disposition:atachment; filename="'. $datumsouboru .'sixfoxdata_30dni.csv"');
    $output = fopen("php://output","w");
    fputcsv($output,array('idd','id','cas','vlhkost spodni','stredni','horni','teplota'));
    $query = "SELECT * FROM data WHERE cas BETWEEN (CURDATE() - INTERVAL 30 DAY) AND CURDATE()";
    $result = $mysqli->query($query);
    while($row = $result->fetch_assoc()) {
        fputcsv($output,$row);
    }
    fclose($output);
    
}
//export všech dat
if(isset($_POST["export_all"])){
    header('Content-Type:text/csv; charset=utf-8');
    header('Content-Disposition:atachment; filename="'.$datumsouboru.'sixfoxdata_all.csv"');
    $output = fopen("php://output","w");
    fputcsv($output,array('idd','id','cas','vlhkost spodni','stredni','horni','teplota'));
    $query = "SELECT * FROM data";
    $result = $mysqli->query($query);
    while($row = $result->fetch_assoc()) {
        fputcsv($output,$row);
    }
    fclose($output);
}




?>
