Join repeated information in a row of the PHP table

0

I need to join in a row of the table when the addresses are repeated with only one address of the three that I have, follow the code below:

                <table class="tableModif">
                    <thead>
                        <tr>
                            <th>Romaneio</th>
                            <th>Placa</th>
                            <th>Cte</th>
                            <th>Data Saida</th>
                            <th>Endereço</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($roteiro->ListaRoteiro($romaneioIni, $romaneioFin, $dataIni, $dataFin, $empIni, $empFin) as $dados) { ?>
                            <tr>
                                <td><?php echo $dados->getRomaneio(); ?></td>
                                <td><?php echo $dados->getPlaca(); ?></td>
                                <td><?php echo $dados->getCte(); ?></td>
                                <td><?php echo ($roteiro->FormataData($dados->getDtSaidaRomaneio())); ?></td>
                                <td><?php echo $dados->getEndereco(); ?></td>
                            </tr>
                        <?php } ?>
                    </tbody>
                </table>

How are you?

Howtostay:

    
asked by anonymous 07.12.2016 / 14:17

1 answer

3

Save the data that has already exited in an array, or as @JuniorNunes said in comment, use groupby :

$repetidos = array();
foreach ($roteiro->ListaRoteiro($romaneioIni, $romaneioFin, $dataIni, $dataFin, $empIni, $empFin) as $dados) {
    $repetidos[$dados->getEndereco()]['romaneio'] = $dados->getRomaneio();
    $repetidos[$dados->getEndereco()]['placa'] = $dados->getPlaca();
    $repetidos[$dados->getEndereco()]['ctes'][] = $dados->getCte();
    $repetidos[$dados->getEndereco()]['data'] = $roteiro->FormataData($dados->getDtSaidaRomaneio());
    $repetidos[$dados->getEndereco()]['endereco'] = $dados->getEndereco();
}

...
<?php
foreach ($repetidos as $dados) { ?>
    <tr>
        <td><?php echo $dados['romaneiro']; ?></td>
        <td><?php echo $dados['placa']; ?></td>
        <td><?php echo implode(', ', $dados['ctes']); ?></td>
        <td><?php echo $dados['data']; ?></td>
        <td><?php echo $dados['endereco']; ?></td>
    </tr>
<?php } ?>
...

Because we have to store all the ctes that a given street (address) can have, we have to loop and store all the addresses before, here all the repeated data will be overwriting ex: $repetidos[$dados->getEndereco()]['romaneio'] , or either with the key $dados->getEndereco() that can be "street 1" getting $repetidos['rua 1']['romaneio'] , for many "street 1" we have the only thing that will happen is to overwrite the result for 'romaneiro', 'placa' etc ... This is going to happen for everyone except for the ctes, these in fact we want to keep all of a certain street.

I do not know if I explained well, but any doubts ask no problem

    
07.12.2016 / 14:28