Group data into a php table

Asked

Viewed 294 times

0

My need is to group the notes in a single row where the OE are repeated for example:

inserir a descrição da imagem aqui

My code below:

<?php 

foreach($class->Lista($empresa,$fatura) as $dados) { 

    $CdFatura = $dados->getFatura();
    $NrNFSe = $dados->getNrNFSe();
    $CdOE = $dados->getCdOe();
    $NrNotaFiscal = $dados->getNrNotafiscal();

    echo '
    <div class="large-12 columns">
        <table>
            <thead>
                <tr>
                    <th>NrNFSe</th>
                    <th>OE</th>
                    <th>NotaFiscal</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>'.$NrNFSe.'</td>
                    <td>'.$CdOE.'</td>
                    <td>'.$NrNotaFiscal.'</td>
                </tr>
            </tbody>
        </table>
    </div>
    ';
}

?>

inserir a descrição da imagem aqui

2 answers

1

Perhaps the following approach will work, but for this it is necessary that the list is ordered by "Cdoe" (as it appears in your example), so the Dexes with the same code will always be one after the other.

        $lista = $class->Lista($empresa, $fatura);

        // executa enquanto a lista tiver valor
        while (!empty($lista)) { 
            // pega o valor atual do ponteiro
            $dados = current($lista);
            // remove este valor da lista
            unset($lista[key($lista)]);

            $CdFatura = $dados->getFatura();
            $NrNFSe = $dados->getNrNFSe();
            $CdOE = $dados->getCdOe();
            $NrNotaFiscal = $dados->getNrNotafiscal();

            // pega o atual index da lista (o anterior já foi removido)
            $nextDados = current($lista);

            // executa este laço enquanto o próximo código existir e
            // for igual ao código atual
            while ($nextDados && $nextDados->getCdOe() == $CdOE) {                
                $NrNotaFiscal .= "<br>{$nextDados->getNrNotafiscal()}";

                // remove o index usado e pega o pŕoximo
                unset($lista[key($lista)]);
                $nextDados = current($lista);
            };

            echo '
            <div class="large-12 columns">
                <table>
                    <thead>
                        <tr>
                            <th>NrNFSe</th>
                            <th>OE</th>
                            <th>NotaFiscal</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>'.$NrNFSe.'</td>
                            <td>'.$CdOE.'</td>
                            <td>'.$NrNotaFiscal.'</td>
                        </tr>
                    </tbody>
                </table>
            </div>
            ';
        }

Obviously, I haven’t been able to test it exactly with your code, but logically, it should work. Before running it is good to analyze the structure of your array and take some precautions so that it does not fall into an infinite loop during the tests (which can lock the machine). For this, you can use an internal counter inside the loop as security.

$i = 0;
while (!empty($lista) && $i < 100) { 
    $i++;

Do it in the other inner loop too.

  • It worked but it made this mistake Call to a member function getCdOe() on boolean in in the while ($nextDados->getCdOe() == $CdOE) {

  • @Kevin. F probably at some point the value of "$data" is coming as null, in this case I believe that validating the element before calling Function should solve. I updated the response with an extra validation in the second while.

  • I understood, but now the notes that come each OE are the same numbers but comes the right amount. For example on OE 36 is coming 4 times the note 1127437.

  • This is because it was calling the 4 times the same main variable ($data), instead of the new variable ($nestDados). Fixed issue.

0


I solved the problem with the code below:

<?php 
$i = 0;

foreach($class->Lista($empresa,$fatura) as $dados) { 

    $i++;

    $CdOE[0] = 0;

    $CdFatura[$i] = $dados->getFatura();
    $NrNFSe[$i] = $dados->getNrNFSe();
    $CdOE[$i] = $dados->getCdOe();
    $NrNotaFiscal[$i] = $dados->getNrNotafiscal();

    if($CdOE[$i] != $CdOE[$i-1]){
        ?>

        <div class="large-12 columns">
            <table>
                <thead>
                    <tr>
                        <th>OE</th>
                        <th>NotaFiscal</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><?php echo $CdOE[$i]; ?></td>
                        <td>
                            <?php 
                                foreach($class->ListaNF($empresa,$CdOE[$i]) as $dados) { 
                                    echo $dados->getNrNotaFiscal().'<br>';
                                }
                            ?>
                        </td>

                    </tr>
                </tbody>
            </table>
            <hr>
        </div>

        <?php
    }
}

Browser other questions tagged

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