gpt4 book ai didi

php - Drupal6 - 输出带有 MENU_CALLBACK 的 CSV

转载 作者:行者123 更新时间:2023-12-04 06:34:22 25 4
gpt4 key购买 nike

我想输出一个带有 drupal 6 模块的 csv 文件。这是我拥有的代码,但它与我的自定义主题中的一些代码和我的模块中的一些代码一起被破解。无论如何我可以将它全部移到我的模块中吗?

///////////////////////////
// csv.module <- a custom module
///////////////////////////
function csv_menu() {

$items = array();

$items['csv/results'] = array (
'page callback' => 'csv_results_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);

return $items;
}

function csv_theme() {
$items = array();

$items['csv_results'] = array(
'arguments' => array(),
);

return $items;
}

function csv_results_page() {
return generate_csv_results();
}

function theme_csv_results() {
return generate_csv_results();
}

function generate_csv_results() {
return "header1,header2,header3,header4\nval1,val2,val3,val4\n";
}


//////////////////////////////
// page-csv-results.tpl.php <- in my theme. I would like it all contained in the module.
//////////////////////////////
<?php
//!TODO: Change Content Type Header
print theme('csv_results');

编辑

以下是有类似问题的任何人的更新版本。感谢chx!
///////////////////////////
// csv.module <- a custom module
///////////////////////////
function csv_menu() {

$items = array();

$items['csv/results'] = array (
'page callback' => 'csv_results_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);

return $items;
}

function csv_results_page() {
//Take a look at the Nikit's response on header stuff. This might be incorrect.
header('Content-Type: text/x-comma-separated-values');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false); // required for certain browser
print generate_csv_results();
}

function generate_csv_results() {
return "header1,header2,header3,header4\nval1,val2,val3,val4\n";
}

最佳答案

这是我导出 csv 文件的简化版本,将此代码添加到 csv_results_page() 中。这里使用了查询,但可以是其他东西(如数组、文件列表等):

/**
* Export to cvs
*/
function _YOURMODULENAME_csv_export() {
$delimiter = "\t"; // This is tab delimiter, can be other

$temp = file_directory_temp();

$file = tempnam(realpath($temp), 'csv');
if (!$fp = fopen($file, 'a')) {
drupal_set_message(t('The file for exported could not be created. Refer to the site administrator'), 'error');
return;
}

$title = "Some title for csv\n"; // you can remove it, if don't want title in csv

$title .= implode($delimiter, array('Column name 1','Column name 2')). "\n"; // Add all columns titles here, it should mutch query result fields below

fwrite($fp, $title);

$query = 'WRITE HERE SOME CODE THAT SHOULD RESULT QUERY AND RETURN fields in order described in $title above';
$result = db_query($query);
while ($data = db_fetch_array($result)) {
$rows = implode($delimiter, $data);
fwrite($fp, $rows."\n");
}

fclose($fp);

$header = array();

$header[] = 'Pragma: no-cache';
$header[] = 'Cache-Control: no-cache, must-revalidate';
$header[] = 'Content-Disposition: attachment; filename="exort_'.date('Ymd',time()).'.csv";'; // This is file name
$header[] = 'Content-Type: text/x-comma-separated-values';
$header[] = 'Content-Length: '.filesize($file);
$header[] = 'Connection: close';

file_transfer($file, $header);
file_delete($file);

return;
}

关于php - Drupal6 - 输出带有 MENU_CALLBACK 的 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5021178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com