Importing CSV files
Posted: Wed Mar 28, 2007 1:31 am
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:
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";
}
?>