I'm trying to write data that I've gotten from an SQL query to a tab-delimited text file. But there is a problem that if there is a blank cell it doesn't register. and just writes the next text in the blank cell.
database data:
id text 2ndText
1 1t1 2t1
2 2t2
3 1t3 2t3
excel output:
id text 2ndText
1 1t1 2t1
2 2t2
3 1t3 2t3
the code we're using is this:
while ($row=mysql_fetch_row($Recordset1))
{
$line='';
$fc=0;
for ($i=0;$i<$fields;$i++)
{
if (substr(strtoupper(mysql_field_name($Recordset1,$i)),0,7)!="DBSTACK")
{
if (strtoupper($_POST['checkbox'.$fc])==strtoupper(mysql_field_name($Recordset1,$i)))
{
$value=$row[$i];
if ((!isset($value)) | ($value == ""))
{
$value = "\t";
} else
{
$value=str_replace('"','""',$value);
$value='"'.$value.'"'."\t";
}
$line.=$value;
}
$fc++;
}
}
$data.=trim($line)."\n";
}
$data=str_replace("\r","",$data);
$TargetFileName="temp/".session_id().time().".xls";
$target=fopen($TargetFileName,"wb");
$XLData=$header."\n".$data;
fwrite($target,$XLData);
fclose($target);
where $Recordset1
is the result of the query
In my search for a solution this came out as a possible solution, but this is a frequently used piece of code so i don't want it to change too much. So is there a simpler solution?
Changing the value to na\t if the cell is empty, could be a solution but that's kind of dirty.
Any ideas?