Scooby08 Posted February 1, 2009 Share Posted February 1, 2009 Trying to figure out how to tell if its the last loop.. Here is basically what I've got for the while loop: <?php $i = 1; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { // check if last loop $i++; } ?> What must I do?? Quote Link to comment https://forums.phpfreaks.com/topic/143342-how-to-tell-if-its-the-last-while-loop/ Share on other sites More sharing options...
NoPHPPhD Posted February 1, 2009 Share Posted February 1, 2009 This maybe? <?php $i=0; $handle = fopen("test.csv", "r"); do { if ($data = fgetcsv($handle, 1000, ",")) { } else { echo 'Last Line Is :'. $i;; break; } $i++; } while ($data != NULL); fclose($handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/143342-how-to-tell-if-its-the-last-while-loop/#findComment-751822 Share on other sites More sharing options...
sasa Posted February 1, 2009 Share Posted February 1, 2009 try <?php $i = 1; $data = fgetcsv($handle, 1000, ","); while ($data !== FALSE) { // check if last loop if (($data1 = fgetcsv($handle, 1000, ",")) == false){ echo 'last loop'; } print_r($data); $i++; $data = $data1; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/143342-how-to-tell-if-its-the-last-while-loop/#findComment-751823 Share on other sites More sharing options...
Psycho Posted February 1, 2009 Share Posted February 1, 2009 If you ONLY need to read/process the last line, there's no need to loop through all of the lines first: //Read file as an array $lines = file('path/to/file/filename.txt'); //Assign the last line, as an array split by CSV, to a variable $last_line = str_getcsv(array_pop($lines), ','); Quote Link to comment https://forums.phpfreaks.com/topic/143342-how-to-tell-if-its-the-last-while-loop/#findComment-751836 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.