Well, the exact code is going to depend on how the data is stored in the file you are trying to import.
I mean, are we talking a file which when viewed in notepade looks like ...
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
So ... 5000+ lines and of course with all different details.
If so, then the example on the page will be right up your alley.
Code: Select all
<?php
$row = 1;
$handle = fopen("test.csv", "r"); // test csv would be the file you are trying to import
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { // this reads one line of the file, and breaks the line at every , into an array of variables
// This part just counts how many pieces that line was made into
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++; // this is just a simple line counter to show what line is being read
for ($c=0; $c < $num; $c++) { // so - for each piece of the line, show the following line.
echo $data[$c] . "<br />\n";
}
} // When we get here, go back and read the next line, right up to the end of the file
fclose($handle);
?>
The internal looping and output makes it all seem a little more complex than it is. The following might be clearer.
Code: Select all
<?php
$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo '## NEW LINE READ ##<br />\n';
foreach($data as $id=>$value) echo '$data[' . $id . "] = $value<br />\n";
}
fclose($handle);
?>
So long as I haven't mis-typed that, you should see a list of data down the screen. The output shows what variable name you need to use inside the for or foreach loop to access each piece of information.
If your data is in a different format, it complicates things a little, but the approach is still pretty much the same.
Hope this helps