Importing CSV

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
patch2112
Forum Commoner
Posts: 86
Joined: Sun Oct 31, 2004 9:44 am
Location: London

Importing CSV

Post by patch2112 »

Hello all,

This is the first time I'm trying to do this, and I just can't get it to work. I have a CSV file in this format...

Code: Select all

Sport_Grey,sport_grey.gif
PoliceBlue,policeblue.gif
Navy_Slate,navy.gif
It's in excel, so the color name is in the first column and the file name in second. There are no header rows and no comma's in any field.

The question is how do I use fgetcsv() for each of these values? I need to populate the DB like...

insert into colours values (color_name,color_file), where these are the two fields contained in the CSV.

Here is what I have now, from the PHP manual...

Code: Select all

$row = 1;
$handle = fopen("colours.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== 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);
But I don't know how to get the individual columns out. I would assume maybe $data[$c][header], but there is no headers.

Any help is appreciated!
Philip
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

The way I always do it is:

Code: Select all

<?php

$lines = file( "file.csv" );

echo count($lines) . " lines\n";

$count = 0;
$data = array();
foreach( $lines as $key=>$val )
{
        $count++;
        // Skip the headers
        if( $count == 1 )
                continue;

        $line= split( ",", $val );
       
        $data[] = $line;
}

?>
That leaves you with the $data array of which each record is an array of the line.
patch2112
Forum Commoner
Posts: 86
Joined: Sun Oct 31, 2004 9:44 am
Location: London

wow

Post by patch2112 »

Thanks so much for the instant reply!

I apologize, but I'm still an amatuer. How do I get the values back out?

I tried...

Code: Select all

foreach ($data as $key=>$val) { echo "Key: $key - Value: $val<br>"; }
after your code

and

Code: Select all

echo "1 "  . $data[0] . "2 " . $data[1] . "<br>";
inside the loop. But there is something wrong here that will be more obvious to you, I'm sure.

Thanks again,
Philip
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

Code: Select all

foreach( $data as $key=>$val )
{
  foreach( $val as $key2=>$val2)
  {
      echo $val2 . ",";
  }
  echo "<br/>";
}
Try that for starters.
patch2112
Forum Commoner
Posts: 86
Joined: Sun Oct 31, 2004 9:44 am
Location: London

Post by patch2112 »

Thanks so much for your help. I'm still not getting it though.

The code gives me all the color names first and then all the file names.

Code: Select all

Sport_Grey,PoliceBlue,Navy,sport_grey.gif,policeblue.gif,navy.gif
But I need to attach them to their counterpart...

Code: Select all

insert into colors (name,file) values (Sport_Grey,sport_grey.gif)
Is there a different way to get the values out of the CSV or do I need some system of attaching an index value to each element for both lines and try to recreate the pairings?

Thanks so much for you help, I owe you a beer...lol.
Philip
patch2112
Forum Commoner
Posts: 86
Joined: Sun Oct 31, 2004 9:44 am
Location: London

Got It

Post by patch2112 »

Thanks again for your help! I just needed some sleep...lol.

Here is how I got your code to work for me...

Code: Select all

$lines = file( "colours.csv" ); 
$count = 0; 
$data = array(); 
foreach( $lines as $key=>$val ) 
{ 
        $count++; 
        // Skip the headers 
        //if( $count == 1 ) 
             //continue; 

        $line= split( ",", $val ); 
        
        $data[] = $line;    
} 

$switch = 1;

foreach( $data as $key=>$val ) 
{ 
  foreach( $val as $key2=>$val2) 
  { 
	if ($switch == 1) { $name = $val2; $switch = 2; }
	else if ($switch == 2)
	{
	  $sql = "insert into colors values (\"auto\",\"$name\",\"$val2\")";
	  echo "$sql<br/>"; 
	  $switch = 1;
	}
  } 
}
Thanks again.
Post Reply