gpt4 book ai didi

php - mysql中打印表后导出为csv

转载 作者:行者123 更新时间:2023-11-29 23:19:46 24 4
gpt4 key购买 nike

花了很多时间试图解决这个问题,但没有结果。用户可以在表单中选择一些过滤器,从而生成一个唯一的表格,并打印到屏幕上。

然后我想要一个按钮来将此特定表导出到 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($_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";
};

}

这不会给出任何错误。它只是重新加载页面。所以我不明白为什么它不起作用。我感谢您的帮助。

最佳答案

部分解决方案

以下代码(您的代码,只是为了测试目的而注释掉了 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";
};

?>

关于php - mysql中打印表后导出为csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27428423/

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