Pregunta

Tengo un script que lee un archivo 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>';
 ?>

Este script acaba de tomar los datos e imprimirlos en una tabla HTML. Sólo quiero ordenar la tabla. Por ejemplo, el csv puede tener estos contenidos

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

Me gustaría que el ser:

0 0 0 0

1 1 1 1

2 2 2 2

3 3 3 3

4 4 4 4

y seguir ... Me algunos me tengo que poner un bucle adicional .. ¿cómo puedo hacerlo?

¿Fue útil?

Solución

Bueno, había leído el archivo CSV en una matriz multidimensional.

Tenga en cuenta que cada línea del archivo CSV es ahora una columna (va de arriba hacia abajo en lugar de izquierda a derecha). Esto se llama transposición filas de columnas.

Para ver una tabla que tendrá que recorrer cada fila, cada columna no. Así se crea un bucle dentro de un bucle, como se muestra aquí:

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

Trate de eso y quiero saber si funciona.

Otros consejos

No estoy muy seguro de cómo se presenta el archivo CSV, pero parece que es posible que necesite para almacenar estos valores en matrices independientes para los diferentes dígitos y luego bucle a través de esas matrices después de haber terminado de leer todo el csv archivo. Se puede mostrar una muestra breve del archivo CSV para que pueda tener una idea de los datos que está leyendo en?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top