【发布时间】: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