Display results then give option to export to .csv?
I have a table on my site that is generated using PHP and a mysql DB. I
would like to have the table displayed with an option below to export the
results to a .csv file.
I was trying to setup a button/link at the bottom that would run the below
php file. However it just exports the entire HTML code into the .csv file
... could someone help me out ?
I run the query then -->
$num_fields = $result->field_count;
$headers = array();
// Creating headers for output files
for ($i = 0; $i < $num_fields; $i++)
{
$headers[] = $result->fetch_fields();
}
$fp = fopen('php://output', 'w');
if ($fp && $result)
{
// name of file with date
$filename = "AccessReport-".date('Y-m-d').".csv";
// Setting header types for csv file.
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='.$filename);
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = $result->fetch_assoc())
{
fputcsv($fp, $row);
}
}
$result->close();
No comments:
Post a Comment