I have a MySQL table, from which I create tsv and csv files. I want to create the tsv file whilst adding imaginary headers to the file. I already use the MySQL column headers as the headers for the file, but I need to add extra imaginary headers not in the MySQL table. My current code creates the file, but I do not know how to go about adding the imaginary headers.
It outputs
name age address
Daniel 24 Carlifornia
Jane 22 New York
I want to output
name age address option1 option2
Daniel 24 Carlifornia anything anything
Jane 22 New York anything anything
Here's my code:
@chmod($export_tsv, 0777);
$fe = @fopen($export_tsv."/export.tsv", "w+");
if($fe){
$somecontent = "";
//$somecontent = "header('Content-type: text/html; charset=utf-8')";
$fields_count = 0;
// fields headers
$db->query($sql_view);
if($row = $db->fetchAssoc()){
foreach($row as $key => $val){
if($fields_count++ > 0) $somecontent .= "\t";
// mysql column headers here
$somecontent .= $key;
}
}
$somecontent .= "
";
$db->query($sql_view);
while($row = $db->fetchAssoc()){
$fields_count = 0;
foreach($row as $key => $val){
if($fields_count++ > 0) $somecontent .= "\t";
//my own special code start
$val = str_replace("
","", $val);
$val = str_replace("","", $val);
$val = str_replace("\t","", $val);
$val = stripslashes($val);
$val = str_replace("chr(13)","", $val);
//my own special code end
$somecontent .= $val;
}
$somecontent .= "
";
}
utf8_encode($somecontent);
$somecontent = mb_convert_encoding($somecontent, 'HTML-ENTITIES', "UTF-8");
// write some content to the opened file.
if (fwrite($fe, utf8_encode($somecontent)) == FALSE)
echo 'file_writing_error'." (export.tsv)";
fclose($fe);
}