我正在尝试仅使用sql查询显示我找到的文件,但不是将这些文件打印到可下载的文件中,而是打印整个html页面。我一直试图提出一个
break;
某处,但我不知道在哪里。
这是我的代码:
if(isset($generate))
{
header("Content-Disposition: attachment; filename=\"generate_reports.csv\";" );
header("Content-type: application/vnd.ms-excel");
$out = fopen("php://output", 'w');
$flag = false;
$result = mysql_query("SELECT location_name From locations");
while(false !== ($row = mysql_fetch_assoc($result)))
{
if(!$flag)
{
$list = array("location_name"=>"location");
fputcsv($out, array_keys($list), ',', '"');
$flag = true;
}
fputcsv($out, array_values($row), ',', '"');
}
fclose($out);
}
修改
我已经添加了打印出的html代码:
<h1>Generate Reports</h1>
<form enctype="multipart/form-data" action="create" method="post">
<table>
<tr>
<td><strong>Materials</strong></td>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db ("design_portal");
$sql = "SELECT material_name FROM materials";
$result = mysql_query($sql);
echo "<td><select name='materials'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['material_name'] . "'>" .
$row['material_name'] . "</option>";
}
echo "</select></td></tr> ";
$sql2 = "SELECT location_name From locations";
$result2 = mysql_query($sql2);
?>
<td><strong>Locations</strong></td>
<?php
echo "<td><select name='locations'>";
while ($row2 = mysql_fetch_array($result2))
{
echo "<option value='" . $row2['location_name'] . "'>" .
$row2['location_name'] . "</option>";
}
echo "</select></td></tr>";
?>
<tr>
<td><button name="submit" type=submit>Generate</button></td>
</tr>
</table>
</form>
答案 0 :(得分:1)
假设if(isset($generate))
- 位只是整个文件的一小部分,我建议您在exit()
之后加fclose($out);
break
仅在循环或开关中有用,并且只执行脚本的其余部分。
您实际上想要完全终止脚本以防止它执行此操作并输出其余的html。