Show CSV in Dadatables with php

Show CSV in Dadatables with php

zeranozerano Posts: 2Questions: 1Answers: 0
edited June 2014 in Free community support

Hi,

I've a CSV table, which I want to print as datatable. But the system does not recognize the header (as manu header. It shows only the content)... I tried much different ways with the <thead> tag, but it does not work... Can anyone help?

<?php  
$datei = 'example.csv';  
echo '<table border="2" cellspacing="0" cellpadding="5" width="500" id="table_id" class="display">';
$open= fopen($datei, "r");
$zeile = 0;
while (($data = fgetcsv($open, 10000, ";"))) {

    echo '<thead><tbody><tr>' . "\n";
    for ( $x = 0; $x < count($data); $x++) {
      if ($zeile == 0)
        echo '<th>'.$data[$x].'</th></thead>' . "\n";
      else
        echo '<td>'.$data[$x].'</td>' . "\n";
      }
      $zeile++;
      echo '</tr></tbody>'. "\n";
}
fclose($open);
echo  '</table>';
?>

Answers

  • zeranozerano Posts: 2Questions: 1Answers: 0
    edited June 2014

    Ok, I resolved it:

    <?php
    $datei = 'example.csv';
    echo '<table border="2" cellspacing="0" cellpadding="5" width="500" id="table_id" class="display">';
    $open = fopen($datei, "r");
    $zeile = 0;
    while (($data = fgetcsv($open, 10000, ";"))) {
        if ($zeile == 0) {
            echo '<thead>';
        }
        elseif ($zeile == 1) {
            echo '</thead><tbody>';
        }
        echo '<tr>' . "\n";
        for ( $x= 0; $x< count($data); $x++) {
          if ($zeile == 0) {
            
            echo '<th>'.$data[$x].'</th>' . "\n";
          } else
            echo '<td>'.$data[$x].'</td>' . "\n";
        }
        $zeile++;
        echo '</tr>'. "\n";
    }
    fclose($open);
    echo  '</tbody></table>';
    ?>
    
This discussion has been closed.