Skip to content

Commit

Permalink
Add a mysqli_result to CSV exporter helper
Browse files Browse the repository at this point in the history
  • Loading branch information
rcarvello committed May 14, 2024
1 parent 03b8cd1 commit 691fe65
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions framework/classes/CSVExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* CSVExporter.
* Exports \mysqli_result to CSV
*
* @package framework/classes
* @filesource framework/classes/CSVExporter.php
* @author Rosario Carvello <[email protected]>
* @version GIT:v1.1.0
* @copyright (c) 2024 Rosario Carvello <[email protected]> - All rights reserved. See License.txt file
* @license BSD Clause 3 License
* @license https://opensource.org/licenses/BSD-3-Clause This software is distributed under BSD-3-Clause Public License
*/

namespace framework\classes;

class CSVExporter
{

/**
* @param \mysqli_result|null $result The given mysqli_result to export as CSV
*/
public function __construct(\mysqli_result $result = null)
{
if (!$result)
return false;
$headers = $result->fetch_fields();
foreach ($headers as $header) {
$head[] = $header->name;
}
$fp = fopen('php://output', 'w');

if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="csv_export.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, array_values($head));
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
die;
}
}
}

0 comments on commit 691fe65

Please sign in to comment.