【问题标题】:Exporting data to CSV将数据导出到 CSV
【发布时间】:2014-08-20 08:45:44
【问题描述】:

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

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

|标题1 |标题2 |标题3 |页眉4 | -> |头350 |

| 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 -->,col350

然后我尝试为每个列输出相关数据.. 应该如下所示

$dataList['col1'][0], $dataList['col2'][0], $dataList['col3'][0], $dataList['col350'][0]

$dataList['col1'][1], $dataList['col2'][1], $dataList['col3'][1], $dataList['col350'][1]

$dataList['col1'][2], $dataList['col2'][2], $dataList['col3'][2], $dataList['col350'][2]

$dataList['col1'][3], $dataList['col2'][3], $dataList['col3'][3], $dataList['col350'][3]

希望这是有道理的,因为它让我的大脑发火..

干杯

马蒂

【问题讨论】:

  • 不小心按了“Enter”!仍在阐述问题。
  • 是的对不起各位,偶然发布了这个问题,仍在编辑中
  • 总是那些懒惰的不必要的反对票......

标签: php


【解决方案1】:

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

然后,您永远不会在“行”循环中重置您的 $row 变量,而是在您的 $line 变量中一次又一次地添加相同的内容,我猜这会导致重复的列。

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

【讨论】:

    【解决方案2】:

    你得到了很好的代码,但我认为你想多了 :-) 在输出数据之前转换数据更容易,因为输出很容易!

    <?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";
    

    【讨论】:

    • 感谢 DoXicK,现在尝试一下,让您知道结果。花了几个小时,试图得到我需要的输出,你的权利,它有点 OTT!
    • 是的,我去过那里很多次 ^_^ 根据我从您的问题中了解到的情况,您只需要将其从基于行转换为基于列:-)
    • 感谢 DoXicK,是的,你是对的,从基于行改为基于列似乎可以解决问题.. :) 干杯芽。
    • 很高兴我能帮上忙 :-)
    猜你喜欢
    • 2017-12-23
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 2013-10-17
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多