Page 1 of 1

Importing CSV files

Posted: Wed Mar 28, 2007 1:31 am
by leewad
Hi

I`m trying to create a script which will import data from a CSV file, I would do it via phpmyadmin but there is no seperating lines, all the data follow on - What I need is the following converting so that after every time the data count ( field count ) gets to 31(amount of fields) then it will class it as a seperating line and start on the next line.

Hope this makes sense and hope someone can help me modify it:

Code: Select all

<?PHP

$fcontents = file ('test.csv');

for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode(",", $line);
$sql = "insert into table_test values ('".
    implode("','", $arr) ."')";

 echo $sql ."<br>\n";

}
?>

Posted: Wed Mar 28, 2007 1:34 am
by s.dot
fgetcsv() would be much better suited to do the job.
there's a nice example on the page too

fgetcsv()

Posted: Wed Mar 28, 2007 3:03 am
by stereofrog
leewad, can you post an example of what the source file looks like?

Posted: Wed Mar 28, 2007 3:08 am
by leewad
I have sorted it now, thanks to scottayy