Jump to content

[SOLVED] Read One Row Of CSV File


refiking

Recommended Posts

Hi guys.  I have no clue how to go about doing this.  I can remove the header row, but I only want to pull one row - row 4.  Here's what I currently have:

 

$handle = fopen("$filename", "r");
     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
     {
IF ($data[1] != "Description"){
Echo '<tr bgcolor="#99CC99" width="475" height="80" align="left">
        		<td width = "475" align="center" > ' . $data[0] . '<br />' . $data[1] . '<br /><a href="' . $data[3] . '" target="_blank">' . $data[2] . '</a><br /> </td>
            </tr>';
}
fclose($handle);

Link to comment
https://forums.phpfreaks.com/topic/153733-solved-read-one-row-of-csv-file/
Share on other sites

I just tried to add a loop snippet and ran into this error:

 

fgetcsv(): 2 is not a valid stream on line 5

 

Here is the code:

$handle = fopen("$filename", "r");
     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
     {
$i=0;
if ($i = 3) {
Echo '<tr bgcolor="' . $bgcolor . '" width="475" height="80" align="left">
        		<td width = "475" align="center" > ' . $data[0] . '<br />' . $data[1] . '<br /><a href="' . $data[3] . '" target="_blank">' . $data[2] . '</a><br /> </td>
            </tr>';
$i++;
}
fclose($handle);
}

Now, I don't get any errors, but I am getting every record in the csv file again.  Here's what I have right now:

 

$handle = fopen("$filename", "r");
     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
     {
$i++;
if ($i = $adnumber) {
Echo '<tr bgcolor="' . $bgcolor . '" width="475" height="80" align="left">
        





<td width = "475" align="center" > ' . $data[0] . '<br />' . $data[1] . '<br /><a href="' . $data[3] . '" target="_blank">' . $data[2] . '</a><br /> </td>
            </tr>';
}
}
fclose($handle);

 

Someone please help... ???

<?php
$handle = fopen("$filename", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
    $i++;
    if ($i == $adnumber) 
    {
         echo '<tr bgcolor="' . $bgcolor . '" width="475" height="80" align="left">
                   <td width = "475" align="center" > ' . $data[0] . '<br />' . $data[1] . '<br /><a href="' . $data[3] . '" target="_blank">' . $data[2] . '</a><br />              </td>
               </tr>';
    }
}
fclose($handle);
?>

Proper indenting helps readability. I assume you've set $adnumber to 4 somewhere else?

You've set $i to 0 before the loop?

And you need to compare using == not =, = just assigned the value over and over. I've fixed it in the above code.. along with the indenting.

Normally I just ignore PM's, but here you go:

Hey Axeia,

 

I really appreciate the help you gave me on that csv record issue.  Is there a way to count the number of rows in a given csv file?  Thanks!

 

 

At the end of the ride $i would have the number of rows

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.