我正在使用agiletoolkit atk4,我想将网格导出到CSV文件,我正在使用下面的代码我遇到的问题是以下细节:
代码:
$grid->menu->addItem(['Export as CSV..', 'icon' => 'add square'], new \atk4\csv\Export($model))->saveAsCSV('file.csv', ['id', 'name']);
问题:错误:找不到类'atk4\csv\Export'。意味着无法在agiletoolkit atk4 文件夹中找到导出库。
我正在使用这篇文章:https ://github.com/atk4/csv
寻求帮助?
注意:我以不同的方式完成了以下是详细信息:
if (isset($_GET['export'])) {
header("Location: exportCSV.php?exportid=".@$_GET['export']);
exit();
}
$button =$grid->menu->addItem(['Export as CSV..', 'icon' => 'add square']);
$button->on('click', null, function ($b) use($app) {
return [$app->jsRedirect(['export' =>'export'])];
//return 'success';
});
在 exportCSV.php 文件中
$query = "SELECT id,name,address FROM tableName;";
$result = mysqli_query($conn, $query) or die("database error:". mysqli_error($conn));
$records = array();
while( $rows = mysqli_fetch_assoc($result) ) {
$records[] = $rows;
}
if(isset($records)) {
$csv_file = "csv_export_".date('Ymd') . ".csv";
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=\"$csv_file\"");
$fh = fopen( 'php://output', 'w' );
$is_coloumn = true;
if(!empty($records)) {
foreach($records as $record) {
if($is_coloumn) {
fputcsv($fh, array_keys($record));
$is_coloumn = false;
}
fputcsv($fh, array_values($record));
}
fclose($fh);
}
exit;
}