douyan3478 2013-05-14 13:35
浏览 56

使用PHP将数据库结果导出为CSV

I've trauled through this site and found tonnes of threads about this same thing but none really help a complete novice like me as I dont understand the answers properly.

I have selected data from a database and displayed in a table on a webpage. I now need a link below this table which will export this data to a CSV file.

one of the answers I have found on this site is: Create a CSV File for a user in PHP

However the answer doesnt really make sense to me and I dont know what to do with the code.

Here is the code from that answer:

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

$array = array(
    array("data11", "data12", "data13"),
    array("data21", "data22", "data23"),
    array("data31", "data32", "data23"));
outputCSV($array);

function outputCSV($data) {
    $outstream = fopen("php://output", "w");
    function __outputCSV(&$vals, $key, $filehandler) {
        fputcsv($filehandler, $vals); // add parameters if you want
    }
    array_walk($data, "__outputCSV", $outstream);
    fclose($outstream);
}

I have so many questions like do I need to create an excel file first? if so does it need to be blank?

what is this?:

$array = array(
    array("data11", "data12", "data13"),
    array("data21", "data22", "data23"),
    array("data31", "data32", "data23"));

is this supposed to be my data? how do I change that to suit the data I have?

Why is the call to the function before the function is defined?

what are these variables in the function ($vals, $key, $filehandler) and where are they created?

How do I use this code because so many people seem to think its perfect.

I really need help from start to finish with this as I am a complete PHP novice

Ive worked on one of the answers below and now I have this code

$i=0;
                        $csv="";
                        for ($a=0; $a<=$count; $a++) {
                            $i++;
                            $csv.=preg_replace("/
/",'',preg_replace("/,/",';',$serveys[$a]['FeedbackName'])).",".
                                  preg_replace("/
/",'',preg_replace("/,/",';',$serveys[$a]['BranchName']));
                            $csv.="
";
                        }

                        if ($i>0) {
                            header("Pragma: public");
                            header("Expires: 0");
                            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                            header("Cache-Control: private",false);
                            header("Content-Type: application/octet-stream");
                            header("Content-Disposition: attachment; filename=\"table.csv\";" );
                            header("Content-Transfer-Encoding: binary");

                            echo $csv;
                        } else {
                            return "Nothing to download!";
                        }

when I run the page though there is no change, the data is still displayed on the page as it should be. but no csv file is created and no errors occurr

  • 写回答

4条回答 默认 最新

  • dsc56927 2013-05-14 13:43
    关注

    The code created a new file object, which outputs to standard output, which then is offered to the user as download because of the headers sent before.

    That IS your data; an array of associative arrays. How you fill it is up to you.

    The call to the function is before it is defined because PHP first parses the file at top level completely, so the function actually will be defined when it is called. This does not apply to functions inside if statements.

    The values to the function are provided by the standard function fputcsv.

    You use the code almost exactly like presented.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)