简体   繁体   English

导出PHP CSV文件列未显示

[英]Export PHP CSV File Columns not displayed

The code below exports data from mysql table correctly but the column names are not displayd in the outputted csv file. 下面的代码正确地从mysql表中导出数据,但列名未显示在输出的csv文件中。 Please help 请帮忙

if(isset($_POST["export"])){
    $branchcode=$_POST['branchcode'];
    $start=$_POST['start'];
    $end=$_POST['end'];
    $sql=mysqli_query($conn,"select tk_file,tk_date,userid,tk_from,tk_to from rfc_timekeeping_history where branchcode='$branchcode' and (tk_date between '$start' and '$end');")or die(mysqli_error());
    $output = "";
    $columns_total =    mysqli_num_fields($sql);
    for ($i = 0; $i < $columns_total; $i++) {
    $heading =  mysqli_fetch_fields($sql, $i);
    $output .= '"'.$heading.'",';
    }
    $output .="\n";
    while ($row = mysqli_fetch_array($sql)) {
    for ($i = 0; $i < $columns_total; $i++) {
    $output .='"'.$row["$i"].'",';
    }
    $output .="\n";
    }
    $filename = "myFile.csv";
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename='.$filename);
    echo $output;
    exit;
}

A different approach using the standard php methods of writing to a csv file ( fputcsv ) and using stdout stream you could try along these lines. 使用标准php方法写入csv文件( fputcsv )和使用stdout流的fputcsv一种方法,您可以尝试以下方法。 The column names are obtained in the first loop before rewinding the recordset to actually process the recordset data. 在倒退记录集以实际处理记录集数据之前,在第一个循环中获取列名。

<?php

    if( isset( $_POST["export"], $_POST['branchcode'], $_POST['start'], $_POST['end'] ) ){

        $filename = "myFile.csv";
        $branchcode=$_POST['branchcode'];
        $start=$_POST['start'];
        $end=$_POST['end'];



        /* store columns names in this array */
        $headers=array();

        /* Run the query */
        $sql = mysqli_query( $conn, "select `tk_file`,`tk_date`,`userid`,`tk_from`,`tk_to` from `rfc_timekeeping_history` where `branchcode`='{$branchcode}' and ( `tk_date between` '{$start}' and '{$end}');")or die(mysqli_error());

        /* fetch the fields */
        $fields = mysqli_fetch_fields( $sql );

        /* add each column name to the array */
        foreach( $fields as $field ) {
            $headers[]=$field->name;
        }


        /* establish basic csv formatting parameters */
        $delimiter=',';
        $enclosure='"';

        /* write output to stdout */
        $output=fopen('php://output','w+');
        fputcsv( $output, $headers, $delimiter, $enclosure );

        /* rewind the recordset to begin processing data */
        mysqli_data_seek( $sql, 0 );

        /* process each row - add to stdout */
        while( $row = mysqli_fetch_array( $sql ) ) {
            fputcsv( $output, $row, $delimiter, $enclosure );
        }

        /* close stdout stream */
        fclose( $output );

        /* send the data */
        header("Content-Type: application/csv");   
        header("Content-Disposition: attachment; filename={$filename}");  
        ob_flush();
        exit();
    }

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM