Skip to content
Advertisement

How to read a CSV file using PHP and display content in Table/DIV?

I am using the following PHP code to read a CSV file with two column (e.g. x,y) and display the content in a table:

<?Php 

echo "<html><body><table border=1>nn";
$f = fopen("data2.csv", "r");
while (($line = fgetcsv($f)) !== false) {
        echo "<tr>";
        foreach ($line as $cell) {
                echo "<td>" . htmlspecialchars($cell) . "</td>";
        }
        echo "<tr>n";
}
fclose($f);
echo "n</table></body></html>";
?> 

However, I am using a new table now (see below) which I want to use to display column A in the cells containing 1/2/3/4 etc and column B in the cells containing 5/6/7/8.

How can I do that?

<table style="text-align: left; width: 406px; height: 200px;"
 border="0" cellpadding="0" cellspacing="0">
  <tbody>
    <tr>
      <td width="25"></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td width="70"></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td colspan="4" rowspan="1">1</td>
      <td>&nbsp;</td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td colspan="3" rowspan="1">5</td>
    </tr>
    <tr>
      <td></td>
      <td colspan="4" rowspan="1">2</td>
      <td>&nbsp;</td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td colspan="3" rowspan="1">6</td>
    </tr>
    <tr>
      <td></td>
      <td colspan="4" rowspan="1">3</td>
      <td>&nbsp;</td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td colspan="3" rowspan="1">7</td>
    </tr>
    <tr>
      <td></td>
      <td colspan="4" rowspan="1">4</td>
      <td>&nbsp;</td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td colspan="3" rowspan="1">8</td>
    </tr>
  </tbody>
</table>

Advertisement

Answer

Use the “fread($f, filesize(filename.kit)” instead of fgetcsv.

<?Php 
echo "<html><body><table border=1>";
$f = fopen("data2.csv", "r");
$fr = fread($f, filesize("data2.csv"));
fclose($f);
$lines = array();
$lines = explode("nr",$fr); // IMPORTANT the delimiter here just the "new line" rn, use what u need instead of... 

for($i=0;$i<count($lines);$i++)
{
    echo "<tr>";
    $cells = array(); 
    $cells = explode(";",$lines[$i]); // use the cell/row delimiter what u need!
    for($k=0;$k<count($cells);$k++)
    {
       echo "<td>".$cells[$k]."</td>";
    }
    // for k end
    echo "</tr>";
}
// for i end
echo "</table></body></html>";
?> 
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement