我有一张显示一些mysql数据的表格,每个条目都有一个复选框来选择单个条目,现在我希望能够将这些选定条目导出到xml或txt文件中,我试过这个:将mysql导出到txt/xml文件并下载它
<?php
if ($_POST['exporttxt']) {
for ($i = 0; $i < count($_POST['checkbox']); $i++) {
$export_id = $checkbox[$i];
$sql = "SELECT * FROM table WHERE id='$export_id'";
$result = mysql_query($sql);
}
$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n";
if ($result->num_rows > 0) {
while ($myrow = $result->fetch_assoc())
{
$output .= "\t<row>\n";
foreach ($myrow as $_name => $_value)
{
$output .= "\t\t<$_name>$_value</$_name>\n";
}
$output .= "\t</row>\n";
}
}
$output .= "</root>";
}
header('content-type: text/xml');
header('content-disposition: attachment; filename=data_export.xml');
echo $output;
exit;
?>
但这并没有工作,任何提示?
我已经改变了代码一点,我现在用的这个
<?php
if ($_POST['exporttxt']) {
for($i=0;$i<count($_POST['checkbox']);$i++){
$export_id = $checkbox[$i];
$text = mysql_query("SELECT code FROM ticket WHERE id='$export_id'");
$text = mysql_fetch_assoc($text);
$text = $text["code"];
ob_end_flush();
header("Content-type: text/plain");
header("Content-disposition: attachment;filename=\"filename.txt\"");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: ".strlen($output).";\n");
echo($text);
}
}
?>
我现在得到我的屏幕上正确的输出,但它不会给我下载的文件?
怎么没工作? – Repox 2011-12-15 21:11:29
你能回应$ export_id以确保你进入循环。对于争论中的计数也不是一个好主意。在for循环之前设置$ count = count($ _ POST ['checkbox']),您将获得更好的性能。 – Robert 2011-12-15 21:13:42