Fill HTML table using 3 PHP Arrays

5

I write about a problem when displaying an HTML table using 3 different arrays.

I explain my problem a bit:

  • I have an arrangement where I get some ID's, these ID's should go in the header of the HTML table, the result will always be 33 records

  • I have another arrangement where I get zones, in total there are 82 records.

  • I have a third arrangement where I get the detail.

  • When I assemble the table in the header I do this:

     $tabla ='<table class="tablalistado compact hover row-border order-column" id="'.$modulo_id.'">
                <thead><tr>
                    <th class="text-center">Zona</th>';
                    for ($encab = 0; $encab < sizeof($array_motsub); $encab++) { 
                        $tabla.='<th class="text-center">'.$array_motsub[$encab]['motivo'].'-'.$array_motsub[$encab]['submotivo'].'</th>';
                    }
    
                $tabla.='</tr>
            </thead><tbody>'; 
    

    There, the header of my table generates me. Then I charge what my areas would be and do the following:

    for ($i=0; $i < sizeof($array_zona);$i++) {            
                $tabla.='<tr><td>'.$array_zona[$i].'</td></tr>';
    }  
    

    There I have a table armed with the header and a column with all the areas something like this:

    Zona D-0 7-0 T-0 T-1 P-0
     1  
     2
     3
     4
     5
     6
    

    Now, there is data in each zone that does not apply to the ID, that is to say, when entering the detail, it should look something like this:

    Zona D-0 7-0 T-0 T-1 P-0
     1    -  30  40  10  - 
     2   15  -    8   3  5
     3   -   -    1   -  -
     4   20   9  10   4  7
     5   13  -   -   78  43
     6   -   12  3    5   3
    

    Where the minus symbol '-' is when there is no data from that area for that ID.

    I have tried a lot of things and it does not work for me, it does not add the script correctly, if there is no data it is placed in the wrong column, the final code I have is like this:

            $tabla ='<table class="tablalistado compact hover row-border order-column" id="'.$modulo_id.'">
                <thead><tr>
                    <th class="text-center">Zona</th>';
                    for ($encab = 0; $encab < sizeof($array_motsub); $encab++) { 
                        $tabla.='<th class="text-center">'.$array_motsub[$encab]['motivo'].'-'.$array_motsub[$encab]['submotivo'].'</th>';
                    }
    
                $tabla.='</tr>
            </thead><tbody>';
    
    
            for ($i=0; $i < sizeof($array_zona);$i++) {            
                $tabla.='<tr><td>'.$array_zona[$i].'</td>';
                foreach ($array as $detalle => $value) {
    
    
                    if( $array_zona[$i] == $value['zona'] && isset($value['indice'])  ){
    
    
                            $tabla.='<td>'.$value['contratos'].'</td>';
    
    
                    }
    
                }
    
                $tabla.='</tr>';
            }
    
    
        $tabla.='</tbody></table>';
        echo $tabla;
    

    EDITO I add a picture of the current result, where you can see that it does not add the values to where it corresponds

    I AGAIN EDIT IMAGE

    I hope you understand my approach a bit, in principle it should not be complicated, but I really have not been able to handle it correctly.

    Greetings and I hope you can help me.

        
    asked by Mixzplit 13.06.2017 в 16:23
    source

    1 answer

    4

    Use the data in your example table to create the code, I hope it helps.

    <?php
    // Varibles de los datos
    $ids = array('D-0','7-0','T-0', 'T-1', 'P-0');
    $zonas = array(1,2,3,4,5,6);
    $detalles = array(
        array('zona' => 1, 'contratos' => 30, 'indice' => '7-0'),
        array('zona' => 1, 'contratos' => 40, 'indice' => 'T-0'),
        array('zona' => 1, 'contratos' => 10, 'indice' => 'T-1'),
        array('zona' => 2, 'contratos' => 15, 'indice' => 'D-0'),
        array('zona' => 2, 'contratos' => 8, 'indice' => 'T-0'),
        array('zona' => 2, 'contratos' => 3, 'indice' => 'T-1'),
        array('zona' => 2, 'contratos' => 5, 'indice' => 'P-0'),
        array('zona' => 3, 'contratos' => 1, 'indice' => 'T-0'),
        array('zona' => 4, 'contratos' => 20, 'indice' => 'D-0'),
        array('zona' => 4, 'contratos' => 9, 'indice' => '7-0'),
        array('zona' => 4, 'contratos' => 10, 'indice' => 'T-0'),
        array('zona' => 4, 'contratos' => 4, 'indice' => 'T-1'),
        array('zona' => 4, 'contratos' => 7, 'indice' => 'P-0'),
        array('zona' => 5, 'contratos' => 13, 'indice' => 'D-0'),
        array('zona' => 5, 'contratos' => 78, 'indice' => 'T-1'),
        array('zona' => 5, 'contratos' => 43, 'indice' => 'P-0'),
        array('zona' => 6, 'contratos' => 12, 'indice' => '7-0'),
        array('zona' => 6, 'contratos' => 3, 'indice' => 'T-0'),
        array('zona' => 6, 'contratos' => 5, 'indice' => 'T-1'),
        array('zona' => 6, 'contratos' => 3, 'indice' => 'P-0'),
    );
    
    // Se crear un matriz apartir del numero de zonas (Filas) y numero de ids (Columas)
    $matriz = array_pad(array(), count($zonas), array_pad(array(), count($ids), "-"));
    
    foreach ($detalles as $key => $value) {
        $z = array_search($value["zona"], $zonas);
        $i = array_search($value["indice"], $ids);
        $matriz[$z][$i] = $value["contratos"];
    }
    ?>
    
    <table>
        <thead>
            <tr>
                <th>Zona</th>
                <?php foreach ($ids as $key => $value) { ?>
                   <th><?php echo $value; ?></th>
                <?php } ?>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($matriz as $key => $value) { ?>
                <tr>
                    <td><?php echo $zonas[$key]; ?></td>
                    <?php foreach ($value as $k => $v) { ?>
                        <td><?php echo $v; ?></td>
                    <?php } ?>
                </tr>
            <?php } ?>
        </tbody>
    </table>
    
        
    answered by 16.06.2017 / 21:44
    source