Error trying to save txt

Asked

Viewed 63 times

2

Everybody, good morning, everybody, I have this code pad:

$delimitador    = ';';
$cerca          = '"';
$VarDataAux     = '';
// Abrir arquivo para leitura
$f = fopen($_FILES['arquivo']['tmp_name'], 'r');
if ($f) {

    // Ler cabecalho do arquivo
    $cabecalho = fgetcsv($f, 0, $delimitador, $cerca);

    // Enquanto nao terminar o arquivo
    while (!feof($f)) {

        // Ler uma linha do arquivo
        $linha = fgetcsv($f, 0, $delimitador, $cerca);
        if (!$linha) {
            continue;
        }

        // Montar registro com valores indexados pelo cabecalho
        $registro = array_combine($cabecalho, $linha);

        $VarLoja      =  $registro['loja'].PHP_EOL;
        $VarData      =  $registro['Dtemi'].PHP_EOL;
        $VarDoc       =  $registro['Doc'].PHP_EOL;
        $VarFonec     =  $registro['Forn'].PHP_EOL;
        $VarNFornec   =  $registro['Nforn'].PHP_EOL;
        $VarCodCont   =  $registro['Codcont'].PHP_EOL;
        $VarValorN    =  $registro['Vlnota'].PHP_EOL;
        $VarValorInss =  $registro['Inss'].PHP_EOL;

        $VarDocFinal =  str_pad($VarDoc,8, '0', STR_PAD_LEFT);

if($VarDataAux != $VarData){
  echo $VarCabecalho =  "M;".$VarLote.";CSV PRODUCAO;".$VarData.PHP_EOL;
  echo $VarLinha =  "*P;".$VarDoc.";".$VarCodCont.";180;".$VarValorInss.";160;".$VarDocFinal." ".$VarNFornec.";".$VarLoja.";;;".$VarData.";;;".$VarLote.PHP_EOL;
}else{
  echo $VarLinha =  "*P;".$VarDoc.";".$VarCodCont.";180;".$VarValorInss.";160;".$VarDocFinal." ".$VarNFornec.";".$VarLoja.";;;".$VarData.";;;".$VarLote.PHP_EOL;
     }

     $VarDataAux = $VarData;

     $name = 'TESTE.txt';
     $text = $VarCabecalho.$VarLinha;
     $file = fopen($name, 'a');
     fwrite($file, $text);
     fclose($file);

   }
  fclose($f);

}

It receives a csv file and handles some information and creates the layout. Until then it’s running all right, I go in the browser and it ta blza, as it has to be, example:

M;4444;CSV PRODUCAO;02/06/2017 
*P;30318 ; ;180;25,81 ;160;030318 JOAO DA SILVA ;2 ;;;02/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;05/06/2017 
*P;30359 ; ;180;4,34 ;160;030359 JOAO DA SILVA ;2 ;;;05/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;06/06/2017 
*P;30364 ; ;180;15,95 ;160;030364 JOAO DA SILVA ;2 ;;;06/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;07/06/2017 
*P;30393 ; ;180;4,37 ;160;030393 JOAO DA SILVA ;2 ;;;07/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;12/06/2017 
*P;30480 ; ;180;19,53 ;160;030480 JOAO DA SILVA ;2 ;;;12/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;16/06/2017 
*P;30529 ; ;180;25,4 ;160;030529 JOAO DA SILVA ;2 ;;;16/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;19/06/2017 
*P;30562 ; ;180;6,75 ;160;030562 JOAO DA SILVA ;2 ;;;19/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;20/06/2017 
*P;30594 ; ;180;15,98 ;160;030594 JOAO DA SILVA ;2 ;;;20/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;22/06/2017 
*P;30620 ; ;180;11,04 ;160;030620 JOAO DA SILVA ;2 ;;;22/06/2017 ;;;4444 
*P;30624 ; ;180;4,94 ;160;030624 JOAO DA SILVA ;2 ;;;22/06/2017 ;;;4444 
*P;30626 ; ;180;14,03 ;160;030626 JOAO DA SILVA ;2 ;;;22/06/2017 ;;;4444 

More as you can see I am exporting it to a txt in this part:

 $name = 'TESTE.txt';
 $text = $VarCabecalho.$VarLinha;
 $file = fopen($name, 'a');
 fwrite($file, $text);
 fclose($file);

Only in my txt this is exporting wrong, is exporting so:

;180;4,34
;160;030359
 JOAO DA SILVA                 
;2
;;;05/06/2017
;;;4444
M;4444;CSV PRODUCAO;06/06/2017

*P;30364
;              
;180;15,95
;160;030364
 JOAO DA SILVA                 
;2
;;;06/06/2017
;;;4444
M;4444;CSV PRODUCAO;07/06/2017

*P;30393
;              
;180;4,37
;160;030393
 JOAO DA SILVA                 
;2
;;;07/06/2017
;;;4444
M;4444;CSV PRODUCAO;12/06/2017

*P;30480
;              
;180;19,53
;160;030480
 JOAO DA SILVA                 
;2
;;;12/06/2017
;;;4444
M;4444;CSV PRODUCAO;16/06/2017

*P;30529
;              
;180;25,4
;160;030529
 JOAO DA SILVA                 
;2
;;;16/06/2017
;;;4444

The first line is respecting ok, only the others it’s breaking us ; I need it to be the same way as this in the browser exit in txt.

I get the csv like this:

loja;Dtemi;Doc;Forn;Nforn;Codcont;Vlnota;Inss
2;02/06/2017;30318;  7688/0;JOAO DA SILVA                 ;              ;1.122,40;25,81
2;05/06/2017;30359;  7688/0;JOAO DA SILVA                 ;              ;188,7;4,34
2;06/06/2017;30364;  7688/0;JOAO DA SILVA                 ;              ;693,6;15,95
2;07/06/2017;30393;  7688/0;JOAO DA SILVA                 ;              ;190,4;4,37
2;12/06/2017;30480;  7688/0;JOAO DA SILVA                 ;              ;849,4;19,53
2;16/06/2017;30529;  7688/0;JOAO DA SILVA                 ;              ;1.104,60;25,4
2;19/06/2017;30562;  7688/0;JOAO DA SILVA                 ;              ;293,84;6,75
2;20/06/2017;30594;  7688/0;JOAO DA SILVA                 ;              ;694,8;15,98
2;22/06/2017;30620;  7688/0;JOAO DA SILVA                 ;              ;480;11,04
2;22/06/2017;30624;  7688/0;JOAO DA SILVA                 ;              ;214,8;4,94
2;22/06/2017;30626;  7688/0;JOAO DA SILVA                 ;              ;610;14,03

I need to deliver the txt like this:

M;4444;CSV PRODUCAO;16/06/2017 
*P;30529 ; ;180;25,4 ;160;030529 JOAO DA SILVA ;2 ;;;16/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;19/06/2017 
*P;30562 ; ;180;6,75 ;160;030562 JOAO DA SILVA ;2 ;;;19/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;20/06/2017 
*P;30594 ; ;180;15,98 ;160;030594 JOAO DA SILVA ;2 ;;;20/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;22/06/2017 
*P;30620 ; ;180;11,04 ;160;030620 JOAO DA SILVA ;2 ;;;22/06/2017 ;;;4444 
*P;30624 ; ;180;4,94 ;160;030624 JOAO DA SILVA ;2 ;;;22/06/2017 ;;;4444 
*P;30626 ; ;180;14,03 ;160;030626 JOAO DA SILVA ;2 ;;;22/06/2017 ;;;4444 

  • When creating variables from data in $registro, why add the PHP_EOL in the end? That’s what’s breaking the line.

  • He has improved, but by doing so he no longer respects this condition: if($Vardataaux != $Vardata){ how could he record this whole return in a single variable ? I think it would solve.

  • If $VarDataAux receives $VarData, I don’t understand why it would make a difference in condition. I’m not sure what that if makes, but you can try to add the PHP_EOL in this if, if it’s the problem, or do the trim of the other variable. It’s hard to say without knowing what the code should do, in fact.

  • if and why: https://answall.com/questions/216797/help-com-condi%C3%A7%C3%A3o-para-montage-layout-csv-com-php

  • Ok, it all makes more sense now. There will always be a header row before the data line?

  • What is $VarLote? Why is this variable used but not defined? And why when you write to TXT you have $VarCabecalho.$VarLinha? It’ll write the headline every time, but that’s not what you want.

  • It comes from a post, and as if it were a user reference, is a number, example: 123 or 456.

  • About: $Varcabecalho.$Varlinha is because I followed the line of another person, but if you are wrong you can change, I need as in the same example, if the date is equal only display the header 1 time, as in this block of the question (I need to deliver txt like this )

Show 3 more comments

1 answer

2


My initial idea was to respond with more structured and better-to-understand code, but you didn’t make it easy. Lots of information omitted and always getting more confused, so the changes you need to make to work are:

  1. Remove the PHP_EOL as suggested in the comments;

    $VarLoja      =  $registro['loja'];
    $VarData      =  $registro['Dtemi'];
    $VarDoc       =  $registro['Doc'];
    $VarFonec     =  $registro['Forn'];
    $VarNFornec   =  $registro['Nforn'];
    $VarCodCont   =  $registro['Codcont'];
    $VarValorN    =  $registro['Vlnota'];
    $VarValorInss =  $registro['Inss'];
    
  2. Fix text that is saved in TXT file. Tue $text = $VarCabecalho.$VarLinha always make record the header row before all the rows and you don’t want that. As you said in the comments, you did it based on someone else’s logic and possibly did not understand what you did. So put $text within the if and remove this wrong;

    if($VarDataAux != $VarData){
      echo $VarCabecalho =  "M;".$VarLote.";CSV PRODUCAO;".$VarData.PHP_EOL;
      echo $VarLinha =  "*P;".$VarDoc.";".$VarCodCont.";180;".$VarValorInss.";160;".$VarDocFinal." ".$VarNFornec.";".$VarLoja.";;;".$VarData.";;;".$VarLote.PHP_EOL;
      $text = $VarCabecalho.$VarLinha;
    }else{
      echo $VarLinha =  "*P;".$VarDoc.";".$VarCodCont.";180;".$VarValorInss.";160;".$VarDocFinal." ".$VarNFornec.";".$VarLoja.";;;".$VarData.";;;".$VarLote.PHP_EOL;
      $text = $VarLinha;
    }
    

With this, your TXT should be generated like this:

M;;CSV PRODUCAO;02/06/2017
*P;30318;              ;180;25,81;160;00030318 JOAO DA SILVA                 ;2;;;02/06/2017;;;
M;;CSV PRODUCAO;05/06/2017
*P;30359;              ;180;4,34;160;00030359 JOAO DA SILVA                 ;2;;;05/06/2017;;;
M;;CSV PRODUCAO;06/06/2017
*P;30364;              ;180;15,95;160;00030364 JOAO DA SILVA                 ;2;;;06/06/2017;;;
M;;CSV PRODUCAO;07/06/2017
*P;30393;              ;180;4,37;160;00030393 JOAO DA SILVA                 ;2;;;07/06/2017;;;
M;;CSV PRODUCAO;12/06/2017
*P;30480;              ;180;19,53;160;00030480 JOAO DA SILVA                 ;2;;;12/06/2017;;;
M;;CSV PRODUCAO;16/06/2017
*P;30529;              ;180;25,4;160;00030529 JOAO DA SILVA                 ;2;;;16/06/2017;;;
M;;CSV PRODUCAO;19/06/2017
*P;30562;              ;180;6,75;160;00030562 JOAO DA SILVA                 ;2;;;19/06/2017;;;
M;;CSV PRODUCAO;20/06/2017
*P;30594;              ;180;15,98;160;00030594 JOAO DA SILVA                 ;2;;;20/06/2017;;;
M;;CSV PRODUCAO;22/06/2017
*P;30620;              ;180;11,04;160;00030620 JOAO DA SILVA                 ;2;;;22/06/2017;;;
*P;30624;              ;180;4,94;160;00030624 JOAO DA SILVA                 ;2;;;22/06/2017;;;
*P;30626;              ;180;14,03;160;00030626 JOAO DA SILVA                 ;2;;;22/06/2017;;;

Defini $VarLote = "" not to generate alerts, since this variable was omitted completely from the question. Therefore the result does not have such value.

If this is not the desired output, it is best to delete the question and create it again, but in a comprehensible and complete way, with all the information, including a minimum, complete and verifiable example.

  • Tip : solved my problem, saved it the way you need it, thanks. $text = $Varlinha;

Browser other questions tagged

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