Add header and footer to txt file generated via phptxt

0

This script generates a txt extracted from MySQL:

<?php

include "conexao.php";

$querymail = mysql_query("select cod,nome,tipo,valor from livros");
        fopen("txt/relatorio.txt", "w+");
        while($data = mysql_fetch_array($querymail)) {
        $log = str_pad($data[0], 10, "0", STR_PAD_LEFT);
        $log1 = str_pad($data[1], 15, " ", STR_PAD_RIGHT);
        $log2 = str_pad($data[2], 10, "0", STR_PAD_LEFT);
                $log3 = str_pad($data[3], 10, "0", STR_PAD_LEFT);
        if (!$savelog = fopen('txt/relatorio.txt', "a")) 
        { exit; }        
        if (!fwrite($savelog, $cod3. $log. $log1. $log2. $cod4b. $cod5b. $cod6b.  $log3. $cod7b. $cod8b."\r\n" ))
        { exit; fclose($savelog); }
        }
 ?>

The generated report looks like this:

001   LIVRO01       AAAA      3200       
002   LIVRO02       AAAA      3200       
003   LIVRO03       AAAA      3200       
004   LIVRO04       AAAA      3200       

I need to insert at the top of the txt file the date and time (081120161226) and in the footer the total workbook and total value, like this:

081120161226

001   LIVRO01       AAAA      3200       
002   LIVRO02       AAAA      1200       
003   LIVRO03       AAAA      1000       
004   LIVRO04       AAAA      2500       

04 6900
    
asked by anonymous 08.11.2016 / 15:39

2 answers

0

Use an array to group the data in the correct sequence, then add everything with join() before writing to the file;

<?php
        while($data = mysql_fetch_array($querymail)) {
        $log = str_pad($data[0], 10, "0", STR_PAD_LEFT);
        $log1 = str_pad($data[1], 15, " ", STR_PAD_RIGHT);
        $log2 = str_pad($data[2], 10, "0", STR_PAD_LEFT);
                $log3 = str_pad($data[3], 10, "0", STR_PAD_LEFT);

        // Agrupe os valores num array ao invés de concatenar as strings, 
        // note o valor em branco na primeira posição
        $savelogs = array('', $cod3, $log, $log1, $log2, $cod4b, $cod5b, $cod6b, $log3, $cod7b, $cod8b);

        // pegue os totais que vai usar
        $totallivro = // total de livros;
        $valortotal = // total de livros;

        // Coloque a data na primeira posição do array
        array_unshift( $savelogs, date('dmYHs') );

        // Adicione mais uma linha em branco
        // e os totais ao final
        $savelogs[] = '';
        $savelogs[] = $totallivro.' '.$valortotal;

        // junte tudo numa string só
        $savelogs = join("\n", $savelogs);

        if (!$savelog = fopen('txt/relatorio.txt', "a")) 
        { exit; }        
        if (!fwrite($savelog, $savelogs."\r\n" ))
        { exit; fclose($savelog); }
        }
    
08.11.2016 / 15:54
0

Use fwrite before while to write the header; during while you will accumulate any variables that you need to total, and at the end, after the end of while , you can use another fwrite to write the total values.

Note: Ideally, you should open the file only once, and only close after everything is written.

    
08.11.2016 / 15:50