Array to PHP table

2

I have the following array:

Array ( [0] => Ficha 001 [nombre] => Ficha 001 [1] => Supermercados [matriz_nombre] => Supermercados [2] => 1000 cc [columna_nombre] => 1000 cc [3] => Heineken [fila_nombre] => Heineken [4] => 15000 [valor_fila_columna] => 15000 ) Array ( [0] => Ficha 001 [nombre] => Ficha 001 [1] => Supermercados [matriz_nombre] => Supermercados [2] => 650 cc [columna_nombre] => 650 cc [3] => Pulp [fila_nombre] => Pulp [4] => 8000 [valor_fila_columna] => 8000 ) Array ( [0] => Ficha 001 [nombre] => Ficha 001 [1] => Supermercados [matriz_nombre] => Supermercados [2] => 300 cc [columna_nombre] => 300 cc [3] => Puro Sol [fila_nombre] => Puro Sol [4] => 7500 [valor_fila_columna] => 7500 )

That has the following structure:

And I need to draw the html table as follows:

It should be emphasized that the values will be loaded dynamically from a database.

    
asked by omelgarejo 22.10.2018 в 14:17
source

2 answers

0

You can go through the array with the data and create an array where you keep the different columns that you will have (without knowing a priori how many or what they are) and another array where you keep the data that you are going to show, saving in each row the name and the data in the position corresponding to the column.

$data = array(
        '1' => array(
            'ficha_nombre' => 'Ficha 001',
            'matriz_nombre' => 'Supermercados',
            'columna_nombre' => '1000 cc',
            'fila_nombre' => 'Heineken',
            'valor_fila_columna' => 15000
        ),
        '2' => array(
            'ficha_nombre' => 'Ficha 001',
            'matriz_nombre' => 'Ficha 001',
            'columna_nombre' => '650 cc',
            'fila_nombre' => 'Pulp',
            'valor_fila_columna' => 8000
        ),
     '3' => array(
            'ficha_nombre' => 'Ficha 001',
            'matriz_nombre' => 'Ficha 001',
            'columna_nombre' => '300 cc',
            'fila_nombre' => 'Puro Sol',
            'valor_fila_columna' => 7500
        )
    );

$a_colsname = array(); 
array_push($a_colsname, "Nombre");
$filas = array();
foreach ($data as $dato) {
   $colname = $dato["columna_nombre"];
    if (!in_array($colname, $a_colsname)) {
       array_push($a_colsname, $colname);
    }
    $fila = array();
    $fila[0] = $dato["fila_nombre"];
    $posicion_col = array_search ( $colname , $a_colsname );
    $fila[$posicion_col] = $dato["valor_fila_columna"];
    $filas[] = $fila;
}
//var_dump($a_colsname);
//var_dump($filas);
echo "<table>";
echo "<tr>";
for ($i=0; $i<count($a_colsname); $i++) {
    echo "<td>".$a_colsname[$i]."</td>";
}
echo "</tr>";
for ($i=0; $i<count($filas); $i++) {
    echo "<tr>";
    for ($j=0; $j<count($a_colsname); $j++){
        if (empty($filas[$i][$j])) {
            echo "<td></td>";          
        } else {
           echo "<td>".$filas[$i][$j]."</td>";
        }
    }
    echo "</tr>";
}
echo "</table>";

Result:

Nombre   1000 cc    650 cc  300 cc
Heineken 15000      
Pulp                8000    
Puro Sol                     7500
    
answered by 22.10.2018 / 16:03
source
3

To start you have the Array wrong.

This is how I would do it:

$array = array(
        '1' => array(
            'ficha_nombre' => 'Ficha 001',
            'matriz_nombre' => 'Supermercados',
            'columna_nombre' => '1000 cc',
            'fila_nombre' => 'Heineken',
            'valor_fila_columna' => 15000
        ),
        '2' => array(
            'ficha_nombre' => 'Ficha 001',
            'matriz_nombre' => 'Ficha 001',
            'columna_nombre' => '650 cc',
            'fila_nombre' => 'Pulp',
            'valor_fila_columna' => 8000
        ),
    );

Where 1 and 2 is each row of Base de Datos .

To visualize it in HTML , what I would do would be to go through the array directly as follows:

<table>
        <tr>
            <td>ficha_nombre</td>
            <td>matriz_nombre</td>
            <td>columna_nombre</td>
            <td>fila_nombre</td>
            <td>valor_fila_columna</td>
        </tr>
        <?php foreach($array as $element): ?>
        <tr>
            <td><?= $element['ficha_nombre']?></td>
            <td><?= $element['matriz_nombre']?></td>
            <td><?= $element['columna_nombre']?></td>
            <td><?= $element['fila_nombre']?></td>
            <td><?= $element['valor_fila_columna']?></td>
        </tr>
        <?php endforeach;?>
    </table>

If you have any questions, do not hesitate to ask!

    
answered by 22.10.2018 в 14:32