0

I'm trying to display a two column, four row CSV file in an HTML table. I have the code below, but it only displays the first row and I don't understand why.

<html>
<head>
    <title>test</title>
</head>
<body>
    <table border=1>
        <?PHP
        $file_handle = fopen("oee1.csv", "r");
        while (!feof($file_handle) ) {
            $line_of_text = fgetcsv($file_handle, 1024);
            echo '<tr><td>' . $line_of_text[0] . '</td><td>' . $line_of_text[1] . '</td></tr>';
        }  
        fclose($file_handle);
        ?>
    </table>
</body>
</html>

The csv file looks like:

test1, 1
test2, 2
test3, 3
test4, 4
3
  • I don't know the exact answer, but I do know that the example on fgetcsv doesn't use feof at all, but just checks the return value of fgetcsv itself. Commented Mar 10, 2014 at 15:25
  • change fgetcsv($file_handle, 1024); to fgetcsv($file_handle, 1024,","); Commented Mar 10, 2014 at 15:27
  • that hasn't fixed the problem I'm afraid! Commented Mar 10, 2014 at 15:29

2 Answers 2

3

You were not iterating through every line. Try something like this:

<!DOCTYPE html>
<html>
    <head>
        <title>+test</title>
    </head>
    <body>
        <?php
            if (($file_handle = fopen("data.csv", "r")) !== false) {
                $str = '';
                $str .= '<table>';
                while (($data = fgetcsv($file_handle, 1024, ",")) !== false) {
                    $str .= '<tr>';
                    foreach ($data as $key => &$value) {
                        $str .= "<td>$value</td>";
                    }
                    $str .= '</tr>';
                }
                fclose($file_handle);
                $str .= '</table>';
                echo $str;
            }
        ?>
    </body>
</html>

output:

<table>
    <tbody>
        <tr>
            <td>test1</td>
            <td>1</td>
        </tr>
        <tr>
            <td>test2</td>
            <td>2</td>
        </tr>
        <tr>
            <td>test3</td>
            <td>3</td>
        </tr>
        <tr>
            <td>test4</td>
            <td>4</td>
        </tr>
    </tbody>
</table>

PS: make sure you have priviledges and your csv is in the smae directory as your php file.

Reference : fgetcsv

6
  • That doesn't seem to do it (although I may be putting the code in the wrong place or not closing brackets properly - I'll keep trying!) Commented Mar 10, 2014 at 15:40
  • can you provide any sample data? Commented Mar 10, 2014 at 15:45
  • question updated with example of csv file (not using real data for now) Commented Mar 10, 2014 at 15:51
  • For some reason this gives me all my data in one row. I think it must be to do with the way new lines are marked in my csv file but I don't really understand why! Commented Mar 10, 2014 at 16:17
  • in this case use ini_set('auto_detect_line_endings',TRUE); read about it on the link i provided Commented Mar 10, 2014 at 16:19
0

try adding a call to fgets($file_handle); in each iteration of the loop, you have to advance the file handle pointer somehow.

1
  • sorry, could you elaborate what you mean by this please? Commented Mar 10, 2014 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.