You are here:  » CSV generating


CSV generating

Submitted by Bay_Oz on Tue, 2011-07-26 12:32 in

Hi David,

I have a csv file which has 15 fields but I want to generate a new one with some filters just for 3 fields.

foreach($record as $k => $v)
{
$fields[] = $v;
}
// construct the line by glueing $fields with the pipe character
$line = implode(",",$fields);
// write $line to the output file terminated in new-line
fwrite($fp,$line."\n");

this one generating some file from original, how can i make it for just 3 field from original file?

Thanks,

Submitted by support on Tue, 2011-07-26 12:37

Hi,

If just 3 fields it's probably best to simply create the $fields array directly rather than as part of a loop, e.g.

$fields[] = $record["FIELD1"];
$fields[] = $record["FIELD2"];
$fields[] = $record["FIELD3"];
// construct the line by glueing $fields with the pipe character
$line = implode(",",$fields);
// write $line to the output file terminated in new-line
fwrite($fp,$line."\n");

...where FIELD1,2.. etc. are the $record keys of the fields you want to include...

Hope this helps!
Cheers,
David.

Submitted by Bay_Oz on Tue, 2011-07-26 13:02

Hi David,

its worked perfect. you are amazing. just a little thing I couldnt add the first line in the csv file. ( like FName,Mobile,Email)

Thanks,

Submitted by support on Tue, 2011-07-26 13:06

Hi,

That would normally be done immediately after opening the file (pointed to by $fp) so outside of myRecordHandler, probably just before your call to MagicParser_parse() you will have something like:

  $fp = fopen("filename.csv","w");

...and then on the next line, add:

  fwrite($fp,"FName,Mobile,Email\n");

Hope this helps!
Cheers,
David.