jb60606 Posted July 3, 2007 Share Posted July 3, 2007 A php script... 1.) connects to a SQL database 2.) extracts multiple IDs from a field we'll call "ID" 3.) uses cURL to open a remote file containing first/last names, city and homepage pertaining to those IDs 4.) cURL saves this file locally as a CSV file. From there, it opens the (csv) file, parses it, and displays particular elements row by row. A rather simplified example: $handle = fopen("data/data.csv", "r") or die ("Can't open file"); while (!feof($handle) ) { $data = @fgetcsv($handle, 200, ","); echo '<tr> echo '<td>First: ' . $data[0] . '</td>'; echo '<td>Last: ' . $data[1] . '</td>'; echo '<td>City: ' . $data[2] . '</td>'; echo '<td>Home: <a href=home/homepage.php$home=' . $data[3] . '</a><img src=img/image.gif></td>'; echo '</tr>'; } @fclose($data); @fclose($handle); echo '</table>'; All works just fine, with the exception of the linked image. It appears for every iteration of the array, as it is supposed to, though oddly appears once more at the end: Imagine the smiley face is the image: JohnSmithChicago EdwardJohnsonEvanston HeatherHillsNorthbrook [/td] That last smiley at the bottom should not be there and it's driving me nuts!!! I thought maybe there was an empty element in the initial array of IDs, but I put a check in the script to make sure there were none. I suppose there could be a blank line in the CSV, but I have no idea on how to check that. Does anyone have any ideas? Thanks Quote Link to comment Share on other sites More sharing options...
jb60606 Posted July 3, 2007 Author Share Posted July 3, 2007 DOH! Nevermind... just stumbled upon the answer! It turns out it was that !feof bit. I guess it will spit out a blank line until it receives some kind of end-of-file pointer, or something like that. I replaced it with: while ( ($data = fgetcsv($handle, 200, ",")) !== false ) { I don't know if it's the most effective solution, but it looks like its getting the job done. The extra line is gone. Find out more from the comments here: http://us2.php.net/feof Quote Link to comment Share on other sites More sharing options...
per1os Posted July 3, 2007 Share Posted July 3, 2007 Sounds like the file has an extra space at the end of it. $handle = fopen("data/data.csv", "r") or die ("Can't open file"); while (!feof($handle) ) { $data = @fgetcsv($handle, 200, ","); if (trim($data) == "" || empty(trim($data))) { break; // kill the loop } echo '<tr> echo '<td>First: ' . $data[0] . '</td>'; echo '<td>Last: ' . $data[1] . '</td>'; echo '<td>City: ' . $data[2] . '</td>'; echo '<td>Home: <a href=home/homepage.php$home=' . $data[3] . '</a><img src=img/image.gif></td>'; echo '</tr>'; } @fclose($data); @fclose($handle); echo '</table>'; Quote Link to comment 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.