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 thePHP_EOL
in the end? That’s what’s breaking the line.– Woss
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.
– Chefe Druida
If
$VarDataAux
receives $VarData
, I don’t understand why it would make a difference in condition. I’m not sure what thatif
makes, but you can try to add thePHP_EOL
in thisif
, if it’s the problem, or do thetrim
of the other variable. It’s hard to say without knowing what the code should do, in fact.– Woss
if and why: https://answall.com/questions/216797/help-com-condi%C3%A7%C3%A3o-para-montage-layout-csv-com-php
– Chefe Druida
Ok, it all makes more sense now. There will always be a header row before the data line?
– Woss
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.– Woss
It comes from a post, and as if it were a user reference, is a number, example: 123 or 456.
– Chefe Druida
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 )
– Chefe Druida