Join repeated information in a PHP table row

Asked

Viewed 211 times

0

I need to join in a table row when the addresses repeat bringing only one address of the three I have, follow 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:

inserir a descrição da imagem aqui

How it should look: inserir a descrição da imagem aqui

  • 1

    I guess this is coming from the bank right? wouldn’t it be better if you use groupby in the query?

  • Yes, serious but would like to treat in php same.

  • The ideal was to do a better query but if you want to do it in php, you will have to filter the result. If the boards are the only repeating field, do one foreach inside another, taking only the board in the inner loop and adding in the array as scoreboard 1, scoreboard 2, scoreboard 3 and so on.

1 answer

3


Save data that has already come out 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 keep all the ctes that a certain street (address) can have, we have to make a loop and save all addresses before, here all the repeated data will be overwritten ex: $repetidos[$dados->getEndereco()]['romaneio'], or with the key $dados->getEndereco() which can be "street 1" getting $repetidos['rua 1']['romaneio'], for many "street 1" that we have the only thing that will happen is to write the result for 'romaneiro', 'plaque' etc.... This will happen for everyone except for Ctes, those in fact we want to guard all of a given street.

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

  • But in this way he could not separate the third item of the table by comma =/

  • How? What comma? @Juniornunes

  • Note in the 2nd image that he placed the 3rd column. He grouped the 8-33090, 8-30100, 8-30107

  • Ha certo @Juniornunes . I will review, my mistake

  • @Miguel It worked, would only have to explain better what was done ? I understood that the data was put in an array, but where it treats the repeated ?

  • Okay, @Kevin. F, I’ll explain in reply

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.