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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.