将数据导出为CSV

时间:2014-08-20 08:45:45

标签: php

我在尝试创建要下载的csv文件时遇到了非常特殊的问题

上传前的csv有这样的数据..

| HEADER1 | HEADER2 | HEADER3 | HEADER4 | - > | HEADER350 |

| col1data | col2data | col3data | col4data | - > ...

| col1data | col2data | col3data | col4data | - > ...

| col1data | col2data | col3data | col4data | - > ...

| col1data | col2data | col3data | col4data | - > ...

尝试"重新创建"输出的csv文件遇到问题。 目前我有我的代码...

$sql = "SELECT id, title, data FROM table ORDER BY ID ASC;";
if(!$result = $mysqli->query($sql))
{ 
    die('There was an error running the query [' . $mysqli->error . ']'); 
}
else
{
  $headers  = array();
  $dataList = array(); 

  while( $row = $result->fetch_assoc() )
  {
    $headers[] = $row['title'];
    $dataList[$row['title']] = json_decode($row['data'], false);
  }

  $head="";
  $line="";

  $totalRows  = count($dataList);   
  if(is_array($dataList))
  {
    // HEADERS
    $firstCount = 1;
    foreach($dataList as $key=>$value)
    {
        if($firstCount==$totalRows){
            $head.= strtoupper($key)."\n\r";    
        }else{
            $head.= strtoupper($key).",";   
        }
        $firstCount++;
    }

    $loop = 0;
    foreach($headers as $headR)
    {
        for($i=0; $i<=$totalRows; $i++)
        {
            if(isset($dataList[$headR][$i])){
                $row.= $dataList[$headR][$i].",";
            }else{
                $row.= ",";
            }
        }
        $line.=$row."\n";
        $loop++;
    }

    $body = $line;

}
print "$head\n$body";
}

所以基本上我首先将标题打印到页面上..

col1,col2,col3,col4,col5,col6 - &gt;,col350

然后我试图为每个col输出相关数据。 这应该是下面的

$ dataList [&#39; col1&#39;] [0],$ dataList [&#39; col2&#39;] [0],$ dataList [&#39; col3&#39;] [0] ,$ dataList [&#39; col350&#39;] [0]

$ dataList [&#39; col1&#39;] [1],$ dataList [&#39; col2&#39;] [1],$ dataList [&#39; col3&#39;] [1] ,$ dataList [&#39; col350&#39;] [1]

$ dataList [&#39; col1&#39;] [2],$ dataList [&#39; col2&#39;] [2],$ dataList [&#39; col3&#39;] [2] ,$ dataList [&#39; col350&#39;] [2]

$ dataList [&#39; col1&#39;] [3],$ dataList [&#39; col2&#39;] [3],$ dataList [&#39; col3&#39;] [3] ,$ dataList [&#39; col350&#39;] [3]

希望这有点意义,因为它会煎炸我的大脑......

欢呼

玛蒂

2 个答案:

答案 0 :(得分:1)

您已经在代码的mysql部分使用了变量$row,稍后,您将尝试使用它来创建临时字符串。

然后,您永远不会在&#34;行&#34;中重置您的$row变量。循环,你继续在你的$line变量中反复添加相同的内容,这导致重复的列我想。

在第二个foreach的最开始添加$row = '';就足够了。

答案 1 :(得分:0)

你在那里得到了很好的代码,但我认为你是在思考这个问题:-)在输出数据之前更容易转换数据,因为输出数据很容易!

<?php

$sql = "SELECT id, title, data FROM table ORDER BY ID ASC;";
if (!$result = $mysqli->query($sql))
    die('There was an error running the query [' . $mysqli->error . ']');

$dataList = array();
$maxDataCount = 0;  
while ($row = $result->fetch_assoc()) {
    $title = strtoupper($row['title']);
    $data = json_decode($row['data'], false);
    // keep the maximum number of data rows. We need to fill in empty gaps later on.
    $maxDataCount = max($maxDataCount,count($data));
    $dataList[] = array( $title, $data );
}
foreach($dataList as $row)
{
    // the first row of the new output array is an array containing headers
    // we just push new headers to that array.
    if (!isset($output[0]))
        $output[0] = array();
    // 0 = title, 1 = data
    $output[0][] = $row[0];
    $data = $row[1];
    // some magic: we append every value to it's corresponding row
    // if the value does not exist, we put NULL
    for($i = 0, $l = $maxDataCount; $i < $l; $i++)
    {
        $idx = $i+1;
        if (!isset($output[$idx]))
            $output[$idx] = array();
        $output[$idx][] = empty($data[$i]) ? NULL : $data[$i];
    }
}

foreach($output as $row)
    echo implode(',',$row) . "\n";