我正在生成包含來自MYSQL數據庫的數據的CSV文件。有一些字段包含布爾值 - 0和1.
我想要的是將布爾值更改爲某個字符串並存儲在生成的文件中。生成CSV時更改值
更新:: CSV代碼
function generateCSV($query, $filename = 'export.csv') {
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = $query;
// Gets the data from the database
$result = mysql_query($sql_query);
$fields_cnt = mysql_num_fields($result);
$schema_insert = '';
for ($i = 0; $i < $fields_cnt; $i++) {
$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
$schema_insert .= $l;
$schema_insert .= $csv_separator;
} // end for
$out = trim(substr($schema_insert, 0, -1));
$out .= $csv_terminated;
// Format the data
while ($row = mysql_fetch_array($result)) {
$schema_insert = '';
for ($j = 0; $j < $fields_cnt; $j++) {
if ($row[$j] == '0' || $row[$j] != '') {
if ($csv_enclosed == '') {
$schema_insert .= $row[$j];
} else {
$schema_insert .= $csv_enclosed .
str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
}
} else {
$schema_insert .= '';
}
if ($j < $fields_cnt - 1) {
$schema_insert .= $csv_separator;
}
} // end for
$out .= $schema_insert;
$out .= $csv_terminated;
} // end while
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out;
}
你可以發佈你用來生成csv的代碼嗎? – 2012-03-09 14:10:21
http://dev.mysql.com/doc/refman/5.0/es/control-flow-functions.html – 2012-03-09 14:12:02
我已經更新了這個問題。請檢查 – 2012-03-09 14:13:00