Hello,
How can i change the CSV header row names?
Greetz
Jord
<?php
$xml = "http://www.nedis.nl/RequestDataOnLine.php?PAR1=HQ 167-3&PAR2=NL&PAR3=USERNAME&PAR4=PASSWORD";
$csv = "NEDIS.csv";
if ( is_file ($csv) ) {
unlink ( $csv) ;
echo "The file was deleted successfully.";
} else { echo "There was an error trying to delete the file.";
}
require("MagicParser.php");
$first = TRUE;
function myRecordHandler($record)
{
global $csv;
global $first;
unset($record["STATUS"]);
unset($record["DATA"]);
unset($record["GROUPING"]);
unset($record["BARCODE"]);
unset($record["CATEGORY"]);
unset($record["CATEGORY/MAINMENU"]);
unset($record["CATEGORY/SUBMENU1"]);
unset($record["CATEGORY/SUBMENU2"]);
unset($record["CATEGORY/SUBMENU3"]);
unset($record["UOM"]);
// strip commas, new-line and carriage return characters from all fields
if ($first)
{
// create the CSV header row on the first time here
$first = FALSE;
$fields = array();
foreach($record as $key => $value)
{
$fields[] = $key;
}
fwrite($csv,implode(";",$fields)."\n");
}
foreach($record as $key => $value)
{
$record[$key] = str_replace(","," ",$value);
$record[$key] = str_replace("\n"," ",$value);
$record[$key] = str_replace("\r"," ",$value);
}
fwrite($csv,implode(";",$record)."\n");
}
$csv = fopen($csv,"a");
if (!$csv) { print "Could not create output file - check permissions!";exit(); }
MagicParser_parse($xml,"myRecordHandler","xml|DATA/");
fclose($csv);
?>
Hello David!
Thank you very much! That did the trick!! :-)
Great support! Thanks again!
Greetzzzz
Jord
Hello Jord,
The easiest thing to do is to create an array of header row names to override the key values, and then use the value from your override array if one exists. To do this, in place of this code from your script:
if ($first)
{
// create the CSV header row on the first time here
$first = FALSE;
$fields = array();
foreach($record as $key => $value)
{
$fields[] = $key;
}
fwrite($csv,implode(";",$fields)."\n");
}
...try this:
if ($first)
{
// create array to override old (original) column names with new names
$columnHeadings["OLD1"] = "NEW1";
$columnHeadings["OLD2"] = "NEW2";
// etc. etc.
// create the CSV header row on the first time here
$first = FALSE;
$fields = array();
foreach($record as $key => $value)
{
if ($columnHeadings[$key])
{
$fields[] = $columnHeadings[$key];
}
else
{
$fields[] = $key;
}
}
fwrite($csv,implode(";",$fields)."\n");
}
Hope this helps!
Cheers,
David.