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

Homie picture Homie · Dec 18, 2012 · Viewed 16.8k times · Source

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>\n\n";
$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>

Answer

insoftservice picture insoftservice · Jun 24, 2014

csv is comma separated values . so its a basic text file that can viewed and access using file() you could use "\n\r" or "\n"

  <?php 
     $data = file("all.csv");
     echo $dataintext = implode("\n",$data);
   ?>

script starts here tester()

    function tester(){
$.post("read.php",{action:'login'},function(data){
    $("#textarea").val(data);
    });
}

html starts here

 <body> 
     <a href="javascript:tester();">read</a> 
     <textarea id="textarea" width="100%" height="100%" ></textarea>
</body>