Add header and footer in txt file generated via phptxt

Asked

Viewed 201 times

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 is thus:

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

I need to enter at the top of the txt file the date and time (081120161226) and at the bottom the total book and total value, so:

081120161226

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

04 6900

2 answers

0

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

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

  • I tested fwrite, but I couldn’t cross-reference with while... Do you have an example? <? php $Fp = fopen('data.txt', 'w'); fwrite($Fp, '1'); fwrite($Fp, 'or'); fwrite($Fp, '2'); fclose($Fp); // the content of 'data.txt' will be 1ou2! ?>

0


Use an array to group the data in the correct sequence, then join it with join() before writing to the archive;

<?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); }
        }
  • I did not succeed in applying this solution, thank you!

  • which error you had @checktech?

  • Link to the working solution: https://eval.in/674353. Replace your variables with strings to facilitate visualization

  • <?php include "connected.php"; $querymail = mysql_query("select Cod,name,type,value from book"); fopen("txt/reporter.txt", "w+"); while($data = mysql_fetch_array($querymail)) {&#xA; $log = str_pad($data[0], 10, "0", STR_PAD_LEFT);&#xA; $log1 = str_pad($data[1], 40, " ", STR_PAD_RIGHT);&#xA; $log2 = str_pad($data[2], 14, "0", STR_PAD_LEFT);&#xA; $log3 = str_pad($data[3], 17, "0", STR_PAD_LEFT);

  • // Group the values into an array instead of concatenating the strings, // note the blank value at first position $savelogs = array(', $cod3, $log, $log1, $log2, $cod4b, $cod5b, $cod6b, $log3, $cod7b, $cod8b); // take the totals you will use $totalbook = '20'; // total books; $total value = '0690'; // total books;

  • // Place the date at the first position of the array array_unshift( $savelogs, date('dmYHs') ); // Add another blank line // and the totals at the end $savelogs[] = '; $savelogs[] = $totalbook. '.$total value; // merge everything into a single string $savelogs = Join(" n", $savelogs); echo $savelogs; ?>

  • I don’t understand. You copied the link code. It worked for you?

  • I copied the changes and made a mistake...

Show 4 more comments

Browser other questions tagged

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