自从很长时间以来一直试图解决这个问题,但没有结果。用户可以在表单中选择一些过滤器,从而在屏幕上打印一个唯一的表格。
然后我想要一个按钮将这个特定的表导出到存储在用户计算机上的csv文件。
if(isset($_POST["askReport"])){
//all the variables
//...
$myQuery = ""SELECT *, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdagU) - pauzetijdU AS totaalurenU, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdag) - pauzetijd AS totaaluren FROM `newRegister` $u $aP $pD $uF $sD"";
$result = $mysqli->query($myQuery);
if($result->num_rows >0){
print '<table><tr><th>.....'; //and all the headers
print '<td>'.$row["user"].'</td>';
..... //and all the other columns and end of row
}
到目前为止,非常好,我得到了一张包含我想要的结果的好桌子。但接下来是棘手的部分 - 如何获得下载此表的按钮?这是我尝试的:(这段代码是在isset中的stil($ _ POST [“askReport”])if语句)
// DOWNLOAD TABEL
print '<form method="POST"><button name=download>download</button></form>';
if(isset($_POST["download"])){
$select = "SELECT *, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdagU) - pauzetijdU AS totaalurenU, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdag) - pauzetijd AS totaaluren FROM `newRegister` $u $aP $pD $uF $sD";
$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $export , $i ) . "\t";
}
while( $row = mysql_fetch_row( $export ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
if ( $data == "" )
{
$data = "\n(0) Records Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=download .xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
};
}
这不会给出任何错误。它只是重新加载页面。所以我无法弄清楚它为什么不起作用。我感谢你的帮助。
答案 0 :(得分:0)
部分解决方案
以下代码(您的代码,只是为了测试目的而注释掉的MySQL)运行正常。我正在运行Apache / 2.4.9(Unix)PHP / 5.5.14
使我下载<?php
和?>
代码的工作就是下载工作。试试这段代码,让我知道它是否有效。
<?php
// DOWNLOAD TABEL
print '<form method="POST"><button name=download>download</button></form>';
if(isset($_POST["download"])){
$select = "SELECT *, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdagU) - pauzetijdU AS totaalurenU, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdag) - pauzetijd AS totaaluren FROM `newRegister` $u $aP $pD $uF $sD";
/* $export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $export , $i ) . "\t";
}
while( $row = mysql_fetch_row( $export ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
*/
if ( $data == "" )
{
$data = "\n(0) Records Found!\n";
}
$header="abc,123";
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=download .xls");
header("Pragma: no-cache");
header("Expires: 0");
echo "$header\n$data";
};
?>