Generate only a Table header with PHP using Foreach

Asked

Viewed 105 times

2

I am working on a simple application that reads XML files, I take the data I do the processing and then I show on the screen the data I want from XML, all via I am showing in a table and the header is if suddenly due is inside the foreache, if I take the foreach table does not show the data, someone knows how to show only the header and only the data repeat ?

I will leave the image of how the table is shown and a part of the code

<?php

error_reporting(0);
ini_set(“display_errors”, 0 );

//Instanciando o OBJ
$xml = new DOMdocument();

//Pegando o TMP do FORM
$arquivo = $_FILES['entXML']['tmp_name'];

    //Total de todos os XML
      $totArquivos = 0;

      $arquivos = $arquivo;

//Navegando no Nodes(Nós)
foreach( $arquivos as$arquivos){

 //Carregando o XML
 $xml ->load($arquivos) or die(" ");
  //Entra dentro do NÓ pai do XML
  $arquivos = $xml->getElementsByTagName( "nfeProc" );

    //Pega Nome
      $nomes = $xml->getElementsByTagName( "xNome" );
      $nome = $nomes->item(0)->nodeValue;
    //Pega CNPJ
      $cnpjS = $xml->getElementsByTagName( "CNPJ" );
      $cnpj = $cnpjS->item(0)->nodeValue;

    //Mostra dados do Emitente
      echo"<header class='text-center'>";
          echo"<div class='col-sm-6 Dempresa'><b>Empresa(Razão):</b> <p>$nome</p></div>";
          echo"<div class='col-sm-4 Dempresa'><b>Cnpj:</b> <p>$cnpj</p></div>";

      echo"</header>";     

        break;  
        }  

          //RECURSO DESESPERADO02 kk
          $arquivo = $arquivo;

//RODANDO TODOS OS ENDEREÇOS DOS ARQUIVOS
for($i = 0; $i < count($arquivo);$i++ ){

        $totArquivos +=count($arquivo); //Total dos arquivos Carregados  

    //Entra dentro do NODE "ide"
foreach( $arquivo as $arquivo[$i]){        

          //Carregando o XML
        $xml ->load($arquivo[$i]) or die(" ");
          //Entra dentro do NÓ pai do XML
        $arquivo[$i] = $xml->getElementsByTagName( "nfeProc" );

  //Pega Data de Emissão da NF
      $chaveS = $xml->getElementsByTagName("infNFe");
      $chave =  $chaveS->item(0)->getAttribute("Id");
  //Pega Data de Emissão da NF                            
      $dataEMS = $xml->getElementsByTagName( "dhEmi" );
      $dataEmi = $dataEMS->item(0)->nodeValue;
  //Pega CFOP                           
      $cfopS = $xml->getElementsByTagName( "CFOP" );
      $cfop = $cfopS->item(0)->nodeValue;
  //Pega Modelo                                    
      $modeloS= $xml->getElementsByTagName( "mod" );
      $modelo = $modeloS->item(0)->nodeValue;
//Mostra Tabela na Tela
      echo "<table  class='table table-striped  table-bordered table-hover table-sm'>";          
              echo"<thead class='thead-light'>";
                echo "<tr>";
                  echo "<th scope='row'>CHAVE DE ACESSO</th>" ;
                  echo "<th class='text-center meu'>Data Emissão</th>" ;
                  echo "<th class='text-center'>CFOP</th>" ;
                  echo "<th>Valor</th>" ;
                  echo "<th>Modelo</th>" ;
                  echo "<th>Status</th>" ;
                echo "</tr>";
          echo'</thead>';


          echo"<tr>";
              echo"<td>$chave</td>";
              echo"<td class='text-center'>".date('d/m/y',strtotime($dataEmi))."</td>";//formato de data brasileiro
              echo"<td class='text-center'>$cfop</td>";
              echo"<td>".number_format($vaPag, 2)."</td>";//Formato moeda com 2 casas(Americano)
              echo"<td  class='text-center'><b>$modelo<b> </td>";
              echo"<td>$motivo </td>";                  
          echo"</tr>";     

      echo "</table>";

TABELA

1 answer

1


One possibility is to have the table encapsulate the for, as this makes possible the unique presentation of the header and gives the iterators the function to build the table body.

  //O inicio da tabela e cabeçalho ficam de fora dos laços for e foreach
  echo "<table  class='table table-striped  table-bordered table-hover table-sm'>";          
            echo"<thead class='thead-light'>";
                echo "<tr>";
                  echo "<th scope='row'>CHAVE DE ACESSO</th>" ;
                  echo "<th class='text-center meu'>Data Emissão</th>" ;
                  echo "<th class='text-center'>CFOP</th>" ;
                  echo "<th>Valor</th>" ;
                  echo "<th>Modelo</th>" ;
                  echo "<th>Status</th>" ;
                echo "</tr>";
          echo'</thead>';

          echo"<tbody>"; //Adicionei a declaração do corpo da tabela

//Mantive o mesmo código em php só removi os espaços e os comentário 
for($i = 0; $i < count($arquivo);$i++ ){
     $totArquivos +=count($arquivo);  
     foreach( $arquivo as $arquivo[$i]){        
        $xml ->load($arquivo[$i]) or die(" ");              
        $arquivo[$i] = $xml->getElementsByTagName( "nfeProc" );
        $chaveS = $xml->getElementsByTagName("infNFe");
        $chave =  $chaveS->item(0)->getAttribute("Id");
        $dataEMS = $xml->getElementsByTagName( "dhEmi" );
        $dataEmi = $dataEMS->item(0)->nodeValue;
        $cfopS = $xml->getElementsByTagName( "CFOP" );
        $cfop = $cfopS->item(0)->nodeValue;
        $modeloS= $xml->getElementsByTagName( "mod" );
        $modelo = $modeloS->item(0)->nodeValue;

          //Essa parte do código é exclusiva para construção das linhas da tabela

            echo"<tr>";
                echo"<td>$chave</td>";
                echo"<td class='text-center'>".date('d/m/y',strtotime($dataEmi))."</td>";
                echo"<td class='text-center'>$cfop</td>";
                echo"<td>".number_format($vaPag, 2)."</td>";
                echo"<td  class='text-center'><b>$modelo<b> </td>";
                echo"<td>$motivo </td>";                  
            echo"</tr>";    

     } //Encerra o bloco do foreach
  } //Encerra o bloco do for

         echo"</tbody>"; //Fecha corpo da tabela

  echo "</table>"; //Conclui a declaração da tabela

To make the encapsulation I practically kept your code only the I structured and removed the comments and spaces between the lines this to take the focus of the PHP code and highlight the goal that is the structure of the table outside the iterators.

I removed from inside the foreach the table declaration, opening tag <table> I put before the for and the closing tag </table> put after the for. I also took the header and Agrupei next to the table opening tag.

I put, on account, the tag <tbody>.

Within the foreach left only the tags responsible for generating table rows.

And here the question code with modification:

<?php

error_reporting(0);
ini_set(“display_errors”, 0 );

//Instanciando o OBJ
$xml = new DOMdocument();

//Pegando o TMP do FORM
$arquivo = $_FILES['entXML']['tmp_name'];

    //Total de todos os XML
      $totArquivos = 0;

      $arquivos = $arquivo;

//Navegando no Nodes(Nós)
foreach( $arquivos as$arquivos){

 //Carregando o XML
 $xml ->load($arquivos) or die(" ");
  //Entra dentro do NÓ pai do XML
  $arquivos = $xml->getElementsByTagName( "nfeProc" );

    //Pega Nome
      $nomes = $xml->getElementsByTagName( "xNome" );
      $nome = $nomes->item(0)->nodeValue;
    //Pega CNPJ
      $cnpjS = $xml->getElementsByTagName( "CNPJ" );
      $cnpj = $cnpjS->item(0)->nodeValue;

    //Mostra dados do Emitente
      echo"<header class='text-center'>";
          echo"<div class='col-sm-6 Dempresa'><b>Empresa(Razão):</b> <p>$nome</p></div>";
          echo"<div class='col-sm-4 Dempresa'><b>Cnpj:</b> <p>$cnpj</p></div>";

      echo"</header>";     

        break;  
        }  

          //Não entendi o porque dessa redundância???
          $arquivo = $arquivo;

  //O inicio da tabela e cabeçalho ficam de fora dos laços for e foreach
  echo "<table  class='table table-striped  table-bordered table-hover table-sm'>";          
            echo"<thead class='thead-light'>";
                echo "<tr>";
                  echo "<th scope='row'>CHAVE DE ACESSO</th>" ;
                  echo "<th class='text-center meu'>Data Emissão</th>" ;
                  echo "<th class='text-center'>CFOP</th>" ;
                  echo "<th>Valor</th>" ;
                  echo "<th>Modelo</th>" ;
                  echo "<th>Status</th>" ;
                echo "</tr>";
          echo'</thead>';

          echo"<tbody>"; //Adicionei a declaração do corpo da tabela

//Fragmento modificado
for($i = 0; $i < count($arquivo);$i++ ){
     $totArquivos +=count($arquivo);  
     foreach( $arquivo as $arquivo[$i]){        
        $xml ->load($arquivo[$i]) or die(" ");              
        $arquivo[$i] = $xml->getElementsByTagName( "nfeProc" );
        $chaveS = $xml->getElementsByTagName("infNFe");
        $chave =  $chaveS->item(0)->getAttribute("Id");
        $dataEMS = $xml->getElementsByTagName( "dhEmi" );
        $dataEmi = $dataEMS->item(0)->nodeValue;
        $cfopS = $xml->getElementsByTagName( "CFOP" );
        $cfop = $cfopS->item(0)->nodeValue;
        $modeloS= $xml->getElementsByTagName( "mod" );
        $modelo = $modeloS->item(0)->nodeValue;

          //Essa parte do código é exclusiva para construção das linhas da tabela

            echo"<tr>";
                echo"<td>$chave</td>";
                echo"<td class='text-center'>".date('d/m/y',strtotime($dataEmi))."</td>";
                echo"<td class='text-center'>$cfop</td>";
                echo"<td>".number_format($vaPag, 2)."</td>";
                echo"<td  class='text-center'><b>$modelo<b> </td>";
                echo"<td>$motivo </td>";                  
            echo"</tr>";    

     } //Encerra o bloco do foreach
  } //Encerra o bloco do for

         echo"</tbody>"; //Fecha corpo da tabela

  echo "</table>"; //Conclui a declaração da tabela
?>
  • 1

    Good morning Mr: Augusto Vasques Raca picking up his logic of explanation became easy to understand, Thank you worked perfectly here. God Bless, I’ll hug le follow on social networks kkk

  • I almost no longer use social network. After Facebook banned my pages I was disappointed. I have a Tweeter account but I don’t use it.

  • Good Morning Right Looking for the Face really did not find tbm n use Tweeter, but anyway Thanks --> Linkedin: https://www.linkedin.com/in/gabriel-santos-498

  • Augusto Goodnight, You can help me with this question I’ve tried anyway here and nothing.... https://answall.com/questions/401608/pular-uma-itera%C3%A7%C3%A3o-com-foreach-when-found-error-no-file-php

Browser other questions tagged

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