简体   繁体   中英

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 (eg 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>

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>

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("\n\r",$fr); // IMPORTANT the delimiter here just the "new line" \r\n, 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>";
?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM