MFA Posted April 2, 2013 Share Posted April 2, 2013 (edited) Hi I have made the code below that outputs the contents of a CSV file into a table. The code works fine however on my server host CGI error log, it says PHP Warning: Invalid argument supplied for foreach() in "my_php_file_name" on line 83 do { $array = fgetcsv($filehandle, 65, ","); echo "<tr>"; foreach ($array as $tableformat) { echo "<td>".$tableformat."</td>"; } echo "</tr>"; $nextrow++; } while ($nextrow <= $lines); $lines refers to the number of lines in the CSV file and $nextrow represents the integer value 0. Although it is working, could someone please explain to me why this message appears so that I can correct it and learn from this experience. Thank you. Edited April 2, 2013 by MFA Quote Link to comment https://forums.phpfreaks.com/topic/276405-foreach-error/ Share on other sites More sharing options...
requinix Posted April 2, 2013 Share Posted April 2, 2013 Have you checked the value of $lines? Is it correct? How are you calculating it? Quote Link to comment https://forums.phpfreaks.com/topic/276405-foreach-error/#findComment-1422373 Share on other sites More sharing options...
nbst Posted April 2, 2013 Share Posted April 2, 2013 Since the warning specifies the invalid argument is located in the foreach, you can ignore the do...while loop and the $nextrow and $lines variables. It has to do with what $array contains. It must not actually be an array, so it's likely either NULL or FALSE, from what I've just read in the docs. Try doing checks on the array variable, testing if it is empty, false, or null before you run the foreach loop which assumes that it is an array. Also, the docs seem to use a while() loop with fgetscsv(). You might want to look into that. nbst Quote Link to comment https://forums.phpfreaks.com/topic/276405-foreach-error/#findComment-1422379 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.