dongtuo1482 2014-12-11 17:02
浏览 26

在mysql中打印表后导出到csv

Been trying to figure this out since a hell of a lot of hours, but without result. A user can select some filters in a form, which result in a unique table, printed to the screen.

Then I want a button to export this specific table to a csv file, stored on the users computer.

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
  }

So far, so good, I get a nice table with the results that I want. But then comes the tricky part - how do I get the button to download this table? Here's what I tried: (this code is stil in the isset($_POST["askReport"]) if statement)

// 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 ) . "
";
        }
        $data = str_replace( "" , "" , $data );

        if ( $data == "" )
        {
            $data = "
(0) Records Found!
";                        
        }

        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=download  .xls");
        header("Pragma: no-cache");
        header("Expires: 0");
        print "$header
$data";         
    };

}   

This doesn't give any errors. It just reloads the page. So I can't figure out why it isn't working. I thank you for your help.

  • 写回答

1条回答 默认 最新

  • douye6812 2014-12-11 17:18
    关注

    PARTIAL SOLUTION

    The following code (your code, just with the MySQL commented out for testing purposes) works fine. I'm running Apache/2.4.9 (Unix) PHP/5.5.14

    The thing that made the downloading work was when I put the <?php and ?> tags in. Try this code and let me know if it works.

    <?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 ) . "
    ";
            }
            $data = str_replace( "" , "" , $data );
    */
    
            if ( $data == "" )
            {
                $data = "
    (0) Records Found!
    ";
            }
    
    $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
    $data";
        };
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存