-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a mysqli_result to CSV exporter helper
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |