Skip to content
PHP Logo

How can PHP be used to generate CSV data for visualization in Dyngraph on the client-side?

To generate CSV data for visualization in Dyngraph on the client-side using PHP, you can create a PHP script that fetches data from a database or any other source, formats it as CSV, and then outputs it to the browser for download. This CSV data can then be used by Dyngraph for visualization.

php
<?php
// Fetch data from a database or any other source
$data = [
    ['Date', 'Sales'],
    ['2022-01-01', 100],
    ['2022-01-02', 150],
    ['2022-01-03', 200],
];

// Output CSV headers
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="data.csv"');

// Output CSV data
$output = fopen('php://output', 'w');
foreach ($data as $row) {
    fputcsv($output, $row);
}
fclose($output);
?>