Problems with using PHP to display CSV file as HTML table -


i'm trying display 2 column, 4 row csv file in html table. have code below, displays first row , 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 

you not iterating through every line. try 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 $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 have priviledges , csv in smae directory php file.

reference : fgetcsv


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -