You are here:  » parsing a CSV file


parsing a CSV file

Submitted by GeXus on Sun, 2007-07-29 23:39 in

I'm trying to parse a CSV file, that is setup as such

2386,7161069
3089,20425651
7496,25473905
2740,28610105
8720,60005696
2870,60009373

I need to be able to insert these values to a database, so I would need to be able to have two insert statements, one for the first value, then another where the value is the first value from the db... So eccentially each row here, needs to be in the same row in the db.

I can read the file, but I can't seem to split it into these two values...

Thanks!

Submitted by support on Mon, 2007-07-30 04:21

Hi,

If your file does not have a header row, and you are therefore using the format string:

csv|44|0|0

(CSV, comma separated, no header row, no text delimiter)

...then within your record handler function the two values should be available as $record["FIELD1"] and $record["FIELD2"]. For example:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    print 
$record["FIELD1"];
    print 
"<br />";
    print 
$record["FIELD2"];
  }
  
MagicParser_parse("data.csv","myRecordHandler","csv|44|0|0");
?>

Hope this helps!
Cheers,
David.