dscrn1974 2014-08-20 08:45
浏览 69
已采纳

将数据导出为CSV

I have very particular problem trying to create my csv file to download

the csv before upload has its data like so..

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

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

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

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

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

when trying to "re-create" the csv file for output im running into problems. currently i have my code like..

$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)."
";    
        }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."
";
        $loop++;
    }

    $body = $line;

}
print "$head
$body";
}

So basically I'm printing the headers first to the page..

col1, col2, col3, col4, col5, col6 -->,col350

Then I'm trying to output the associated data for each col.. which should be like below

$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]

hopefully that makes some sense, because its frying my brain..

cheers

Marty

  • 写回答

2条回答 默认 最新

  • duangai1368 2014-08-20 09:18
    关注

    Nice piece of code you got there, but i think you are overthinking this :-) It's easier to just transform the data before you are outputting it, since outputting it is easy!

    <?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) . "
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿