You are here:  » Create one CSV file from several


Create one CSV file from several

Submitted by jonny5 on Thu, 2012-02-09 09:57 in

Hi David, I have 3 csv files that I want to combine into one file and have the same specific structure , so the same headers.

just wondering if you have some sort of template I could work/start from and i could then have a play.

cheers

Submitted by support on Thu, 2012-02-09 11:09

Hi Jonny,

Sure - the basic structure (to output CSV to the client) would be something like this:

<?php
  header
("Content-Type: text/plain");
  require(
"MagicParser.php");
  function 
csvsafe($text)
  {
    return 
str_replace(array(",","\n","\r")," ",$text);
  }
  function 
myRecordHandler($record)
  {
    global 
$fields;
    
$row = array();
    foreach(
$fields as $field)
    {
      
$row[] = csvsafe($record[$field]);
    }
    print 
implode(",",$row)."\n";
  }
  
// print header row
  
print "Field1,Field2,Field3,Field4\n";
  
$fields = array("ABCD","EFGH","IJKL","MNOP");
  
MagicParser_parse("file1.csv","myRecordHandler","csv|44|1|0");
  
$fields = array("FIELD1","FIELD2","FIELD3","FIELD4");
  
MagicParser_parse("file2.csv","myRecordHandler","csv|44|1|0");
  
$fields = array("FieldA","FieldB","FieldC","FieldD");
  
MagicParser_parse("file3.csv","myRecordHandler","csv|44|1|0");
?>

The $fields array for each feed contains the 4 field names from each CSV file that map to the 4 fields you want to output (corresponding to the header row Field1,Field2 etc.). When myRecordHandler is called, the contents of each of those fields is then output.

If you're not sure of the Format String required for each of your feeds, check the documentation here or of course upload to the demo tool which will auto-detect the format just like when registering a feed in Price Tapestry...

Hope this helps!
Cheers,
David.

Submitted by jonny5 on Thu, 2012-02-09 11:54

ok cheers.
right have the below at the moment where its just 2 files at the moment to see it i can get it to work, ive added all the fields , some left black which i assume is ok and some repeated .
how would i go about testing it , how would i execute it , would i use a cron job or can i do it locally or within the browser?

<?php
  header("Content-Type: text/plain");
  require("MagicParser.php");
  function csvsafe($text)
  {
    return str_replace(array(",","\n","\r")," ",$text);
  }
  function myRecordHandler($record)
  {
    global $fields;
    $row = array();
    foreach($fields as $field)
    {
      $row[] = csvsafe($record[$field]);
    }
    print implode(",",$row)."\n";
  }
  // print header row
  print "coupon_description,coupon_excerpt,coupon_category,coupon_tag,coupon_type,stores,date,author,slug
,coupon_code,expire_date,print_url,id,coupon_aff_url,store_aff_url,store_url,store_desc\n";
  $fields = array
("description","description","","","","program_name","start_date","","","code","end_date","","","url""url","url","program
_name",);
  MagicParser_parse("file1.csv","myRecordHandler","csv|44|1|34");
  $fields = array("Offer","Offer","","","","Merchant","Start Date","","","Code","Expiry","","","Signup Link","Signup
Link","Signup Link","Merchant",);
  MagicParser_parse("file2.csv","myRecordHandler","csv|44|1|0");
?>

Submitted by support on Thu, 2012-02-09 12:30

Hi Jonny,

To handle empty fields gracefully, replace this line in the code:

$row[] = csvsafe($record[$field]);

with:

if ($field)
{
  $row[] = csvsafe($record[$field]);
}
else
{
  $row[] = "";
}

To run the script, create a new folder on your server and save your code there using a suitable filename e.g.
merge.php.

Also put in the folder a copy of MagicParser.php and your file1.csv, file2.csv files etc. as Magic Parser will look for them in the same folder as your script.

Finally, browse to merge.php to view / download the combined CSV...

Cheers
David.

Submitted by jonny5 on Thu, 2012-02-09 12:35

cheers , i just get a HTTP 500 internal server error

Submitted by support on Thu, 2012-02-09 13:16

Hi jonny,

There was a superfluous comma just inside the closing bracket on this line:

  $fields = array
("description","description","","","","program_name","start_date","","","code","end_date","","","url""url","url","program_name",);

...which should be:

  $fields = array
("description","description","","","","program_name","start_date","","","code","end_date","","","url""url","url","program_name");

However that should have caused a parse rather than internal server error but some PHP configurations may use that for security reasons. If still not working after the above correction; email me your script and i'll check it out...

Cheers,
David.

Submitted by jonny5 on Thu, 2012-02-09 14:35

All working now from the file you sent me
Many thanks