Domanda

Ho uno script che leggere un file CSV.

 <?php
 echo '<table border="0" cellspacing="1" cellpadding="1" class="sortable" border="1"><caption>Title Here</caption>
 <thead><tr><th class="header">Time:</th><th class="header">Value 1:</th><th class="header">Value 2:</th><th class="header">Value 3:</td class="header"><th class="header">Value 4:</th><th class="header">Value 5:</th><th class="header">Value 6:</th><th class="header">Value 7:</th><th class="header">Value 8:</th><th class="header">Value 9:</th></tr></thead><tbody><tr>';
 $row = 1;
 if (($handle = fopen("data.csv", "r")) !== FALSE) {
   while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
     $num = count($data);   
     $row++;
     for ($c=0; $c < $num; $c++) {
        if ($c==9) { echo "<td>".$data[$c] ."</td></tr><tr>";}
        else  {echo "<td>".$data[$c] ."</td>"; }
     }
   }
   fclose($handle);
 }
 echo '</tbody></table>';
 ?>

Questo script basta prendere i dati e li stampa in una tabella HTML. Voglio solo di riorganizzare il tavolo. Ad esempio, il CSV può avere questi contenuti

0 1 2 3 4 5 6 7

0 1 2 3 4 5 6 7

0 1 2 3 4 5 6 7

0 1 2 3 4 5 6 7

Vorrei che la rivela:

0 0 0 0

1 1 1 1

2 2 2 2

3 3 3 3

4 4 4 4

e andare avanti ... Ho un po 'devo mettere un ciclo aggiuntivo .. come posso fare?

È stato utile?

Soluzione

Ebbene si aveva letto il file CSV in un array multidimensionale.

Si consideri che ogni riga del file CSV è ora una colonna (va verso il basso invece che da sinistra a destra). Questo si chiama Trasporre le righe per colonne.

Per una tabella avrete bisogno di un ciclo tra ogni riga, ogni colonna non. Così si crea un loop all'interno di un ciclo, come illustrato di seguito:

<table border="0" cellspacing="1" cellpadding="1" class="sortable" border="1"><caption>Title Here</caption>
     <thead><tr><th class="header">Time:</th><th class="header">Value 1:</th><th class="header">Value 2:</th><th class="header">Value 3:</td class="header"><th class="header">Value 4:</th><th class="header">Value 5:</th><th class="header">Value 6:</th><th class="header">Value 7:</th><th class="header">Value 8:</th><th class="header">Value 9:</th></tr></thead><tbody>
<?php
     #read CSV file
     if (($handle = fopen("data.csv", "r")) !== FALSE) {
       $mycsv = array();
       while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) $mycsv[] = $data;
       fclose($handle);


     #Find the length of the transposed row

     $row_length = count($mycsv);

     #Loop through each row (or each line in the csv) and output all the columns for that row
     foreach($mycsv[0] as $col_num => $col)
     {
        echo "<tr>";
        for($x=0; $x<$row_length; $x++)
           echo "<td>".$mycsv[$x][$col_num]."</td>";


        echo "</tr>";
     }

  }
?>
  </tbody></table>

provare che fuori e fatemi sapere se funziona.

Altri suggerimenti

Non sono esattamente sicuro di come il file CSV è disposto, ma sembra che potrebbe essere necessario per memorizzare questi valori in array separati per le diverse cifre e quindi un ciclo tra quelle array dopo aver finito di leggere l'intero csv file. Si può mostrare un breve esempio di file CSV in modo da poter avere un'idea dei dati che state leggendo in?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top