miseleigh Posted September 24, 2007 Share Posted September 24, 2007 I'm trying to read in a csv file and put it into arrays. Shouldn't be too hard... but for some reason, my loop only reads in the first line of the .csv file. Any suggestions? $eday_count = 0; if(($fp=fopen($event_fn, 'r'))!=null) { while(($line=fgetcsv($fp,500,",","\""))!==FALSE) { $event_day[$eday_count]['type'] = $line[$sch_TYPE]; $event_day[$eday_count]['msg'] = $line[$sch_MSG]; $eday_count++; } } After this, my $event_day array only has one entry, and it should have several. Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/70491-solved-my-fgetcsv-loop-will-only-read-one-line/ Share on other sites More sharing options...
BlueSkyIS Posted September 24, 2007 Share Posted September 24, 2007 remove all but the resource handle from the fgetcsv call and see what happens? fgetcsv($fp); Quote Link to comment https://forums.phpfreaks.com/topic/70491-solved-my-fgetcsv-loop-will-only-read-one-line/#findComment-354099 Share on other sites More sharing options...
miseleigh Posted September 24, 2007 Author Share Posted September 24, 2007 I tried this: $line=fgetcsv($fp); print_r($line); The result looks like there's no endline at the end of each line, so fgetcsv is reading the entire file as if it's a single line. The first field of a line is appended onto the last field of the line above it into a single array field. How can I fix this? (preferably without editing the .csv, but I may have to...) Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/70491-solved-my-fgetcsv-loop-will-only-read-one-line/#findComment-354113 Share on other sites More sharing options...
miseleigh Posted September 24, 2007 Author Share Posted September 24, 2007 Just a little more info: The fgetcsv() output looks like this (one function call, should only return one line): Array ( [0] => 9/15/07 [1] => 0:00 [2] => 0:00 [3] => Public [4] => test 9/15/07 [5] => 0:00 [6] => 0:00 [7] => Public [8] => test2 9/20/07 [9] => 0:00 [10] => 5:07 [11] => Private [12] => sg 9/23/07 [13] => 0:01 [14] => 5:07 [15] => Private [16] => sg 9/23/07 [17] => 14:04 [18] => 5:07 [19] => Private [20] => sg) When it should be like this (multiple calls shown): Array ( [0] => 9/15/07 [1] => 0:00 [2] => 0:00 [3] => Public [4] => test ) Array ( [0] => 9/15/07 [1] => 0:00 [2] => 0:00 [3] => Public [4] => test2 ) Array ( [0] => 9/20/07 [1] => 0:00 [2] => 5:07 [3] => Private [4] => sg ) Array ( [0] => 9/23/07 [1] => 0:01 [2] => 5:07 [3] => Private [4] => sg ) Array ( [0] => 9/23/07 [1] => 14:04 [2] => 5:07 [3] => Private [4] => sg ) I'm still quite confused. Does anyone know why it might be doing this? What line delimeter does a .csv use & what is fgetcsv expecting? Could it have something to do with the fact that the .csv was created on a PC and the script is running on a Mac? I would really appreciate some help with this one. Quote Link to comment https://forums.phpfreaks.com/topic/70491-solved-my-fgetcsv-loop-will-only-read-one-line/#findComment-354217 Share on other sites More sharing options...
miseleigh Posted September 24, 2007 Author Share Posted September 24, 2007 Alright, it's been figured out, in case anyone else has a similar problem: DON'T USE EXCEL FOR YOUR CSV FILES! Somehow Excel messed up the newlines in the file, so it exploded. Recreate your file in notepad or something similar. Quote Link to comment https://forums.phpfreaks.com/topic/70491-solved-my-fgetcsv-loop-will-only-read-one-line/#findComment-354248 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.