You are here:  » Problem Parsing CSV


Problem Parsing CSV

Submitted by Dark Knight on Tue, 2008-09-16 19:11 in

HI,

I have a csv feed that I want to parse and then enter into a databas via mySQL and also have as a csv file for import in excel.

The database part is not a problem nor is the csv text file, my problem seems to be that the feed has a timestamp just before the field names header and this fouls up the parsing. Does any one have any suggestuons to get around this?

Some sample data from the feed below:-

Array ( [FIELD1] => feed created:Tue Sep 16 19:54:03 BST 2008 ) Array ( [FIELD1] => products_id [FIELD2] => model [FIELD3] => ean [FIELD4] => name [FIELD5] => description [FIELD6] => dimension [FIELD7] => price [FIELD8] => delivery_code [FIELD9] => quantity [FIELD10] => categories [FIELD11] => options [FIELD12] => image_url ) Array ( [FIELD1] => "4215" [FIELD2] => "WC213" [FIELD3] => "5055071609367" [FIELD4] => "Flower Ribbon Hanger " [FIELD5] => "Each Flower ribbon hanger is priced and sold separately and comes packaged individually in a decorative card box.

Please note when low in stock some designs may be unavailable.
" [FIELD6] => "Length 80cm [FIELD7] => Bead Width 12cm" [FIELD8] => "1.91" [FIELD9] => "B" [FIELD10] => "338" [FIELD11] => "112|100513" [FIELD12] => "" [FIELD13] => "http://www.xxx.co.uk/gifts/images/WC213_001.jpg" )

And my current script:

<?php
  
require("MagicParser.php");
 
$csv_all "/home/rdlsafet/public_html/xxxxxx/new_csv_feed.csv";
  if (
file_exists("/home/rdlsafet/public_html/xxxx/new_csv_feed.csv")){
    print 
" This File already exists - deleting orginal products file...";
    
//Delete file if already exists
    
unlink ("/home/rdlsafet/public_html/xxxxnew_csv_feed.csv");
    print 
"<br>Building new Product detail csv file...., <br />";
}
  function 
myRecordHandler($record)
  {
    
// This is where you write your code to process each record, such as loading a database
    // You can display the record contents using PHP's internal print_r() function:
    
print_r($record);
    
// The following code will print out each field in your sample data:
   /**
*  print $record["products_id"];
*     print $record["model"];
*     print $record["ean"];
*     print $record["name"];
*     print $record["description"];
*     print $record["dimensions"];
*     print $record["price"];
*     print $record["delivery_code"];
*     print $record["quantity"];
*     print $record["categories"];
*     print $record["options"];
*     print $record["image_url"];
*/
    
global $csv_all;
 
fwrite($csv_all,implode(",",$record));
  }
  
$csv_all fopen($csv_all,"a");
  if (!
$csv_all) { print "Could not create output file - check permissions!";exit(); }
 
MagicParser_parse("http://www.xxx.co.uk/gifts/feed_csv_products.php?email=xxx&passwd=dxxx&action=full","myRecordHandler","csv|44|0|0");
  
fclose($csv_all);
?>

Thanks in advance

John

Submitted by support on Wed, 2008-09-17 08:23

Hello John,

You can make Magic Parser skip the first line of the file (if this is where the timestamp appears) as
an optional 5th parameter in the Format String; so if you use:

csv|44|0|0|1

...the first line will be ignored and parsing will work as normal.

Hope this helps,
Regards,
David.

Submitted by Dark Knight on Thu, 2008-09-18 22:35

Many Thanks David,

All working well now thanks to you

regards

John