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