<?php
// Include the database connection
include 'db_connection.php';

// Set headers for CSV download
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=assets_export_' . date('Y-m-d') . '.csv');

// Create output stream
$output = fopen('php://output', 'w');

// Add CSV headers
fputcsv($output, array('ID', 'Description', 'Amount', 'Date Added'));

// Fetch data from database
$query = "SELECT * FROM assets ORDER BY date DESC";
$result = $conn->query($query);

// Add data to CSV
while ($row = $result->fetch_assoc()) {
    fputcsv($output, array(
        $row['id'],
        $row['description'],
        '$' . number_format($row['amount'], 2),
        $row['date']
    ));
}

// Close connection
fclose($output);
$conn->close();
exit();
?>