Re: Delete first line of a csv file if it is the headers
| From: | Richard Lynch | Date: | Wed, 02 May 2007 21:38:05 +0000 |
| Subject: | Re: Delete first line of a csv file if it is the headers | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-254287@lists.php.net to get a copy of this message | ||
Do one, and only one, fgetcsv() before you entere the loop?
Or, if only SOME people add the header lines to their CSV file, check
if one of the numeric fields is actually numeric, and skip any "bad"
lines.
You could even keep a counter going of which line you are on, and give
an error message if you skip any line other than the first line.
It's good to have a counter anyway, as any error message you want to
dump out for an invalid input (and you WILL get them) ought to at
least narrow it down to line and column, so somebody can actually
figure out what's wrong, eh?
On Sun, April 29, 2007 11:06 pm, Richard Kurth wrote:
> The below script will parse a csv file to a database I need to
> remove
> the first line because it is the header files and I don't need them.
> How can
> I test for that and remove them.
>
> $file = fopen($_POST['copy'], 'r') or $message .= "Could not
> open" .
> $_POST[copy] . "for reading.<BR>\n";
> while (!feof($file)){
> # We could easily be importing 10,000 records.
> Give us
> some time here, okay?
> set_time_limit(30);
> $fields = fgetcsv($file, 1000000);
>
> if ($fields && count($fields)){
> $recordcount++;
>
> # Wipe out every possible field, in case they
> have
> some missing:
> reset($possiblefields);
> while (list(,$fieldname) =
> each($possiblefields)){
> $$fieldname = '';
> }
>
> reset($_POST['fieldorder']);
> $fieldcount = 0;
> $notes = '';
> while (list(,$field) = each($fields)){
> $fieldname =
> $_POST['fieldorder'][$fieldcount++];
> if ($fieldname == 'notes'){
> $notes .= addslashes("$field\n");
> }
> elseif ($fieldname == '-- ignore --'){
> # ignore it
> }
> else{
> # Note that file data is not
> Magic-Quoted:
> $$fieldname = addslashes($field);
> }
> }
> require 'importinsert.php';
> }
>
--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?