Jump to content

[SOLVED] Odd problem


jb60606

Recommended Posts

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 ;D

EdwardJohnsonEvanston ;D

HeatherHillsNorthbrook ;D

[/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

Link to comment
https://forums.phpfreaks.com/topic/58312-solved-odd-problem/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/58312-solved-odd-problem/#findComment-289129
Share on other sites

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>';

Link to comment
https://forums.phpfreaks.com/topic/58312-solved-odd-problem/#findComment-289130
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.