View Single Post
Old 02-21-2013, 07:30 AM   PM User | #1
Tidus
New Coder

 
Join Date: Aug 2002
Posts: 66
Thanks: 2
Thanked 0 Times in 0 Posts
Tidus is an unknown quantity at this point
fgetcsv OR SplFileObject HELP!!

Hi guys.

I have a CSV file that I am trying to import to a mysql database.

The issue i am having is that the system we export the CSV files from seems to enclose SOME 'columns' with a " character. I am assuming it does this when the column contains a , character (for example in a name like DOE, JANE) that I dont want to be split into a separate column.

I can't seem to work out the correct code to make this happen. Here is an example of a line.

"ABEL, TAMMY 454454","End of: ABEL, TAMMY 454454",QP544454,28/10/2012 11:41,"0811 unlawfully use, possess","STEPHENS, JEREMY 54544454",LINK OPERATIONS,Located details incorrect,Entity: FORD FALCON Reg #: Colour: White

So it only seems to include the " when a , is necessary in the column.

My code looks like this;

PHP Code:
$row 1;
if ((
$handle fopen("test.csv""r")) !== FALSE) {
    while ((
$data fgetcsv($handle1000",")) !== FALSE) {
        
$num count($data);
        echo 
"<p> $num fields in line $row: <br /></p>\n";
        
$row++;
        for (
$c=0$c $num$c++) {
            echo 
$data[$c] . "<br />\n";
        }
    }
    
fclose($handle);

It doesn't seem to be parsing correctly...

I've also tried it with SplFileObject with similar results - i'm happy to use either.

HELP!?!

Thanks heaps
Tidus is offline   Reply With Quote