Format csv via php

Asked

Viewed 520 times

1

I have the following code, where it generates a csv spreadsheet.

<?php
    require_once ('dbacess.php');
    // output headers so that the file is downloaded rather than displayed
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=data.csv');

    // create a file pointer connected to the output stream
    $output = fopen('php://output', 'w');

    // output the column headings

    // fetch the data

    $rows = sprintf('select * from ivr_contatos, ivr_campanha,ivr_business where ivr_contatos.campanha = ivr_campanha.id and ivr_business.idvisita = ivr_contatos.codigo and ivr_contatos.status = 0 and tentativas >= qtdtentativas');
    $linha = Populator::ConsultaDB($rows);
    // loop over the rows, outputting them
    fputcsv($output,array("CHAMADO","NOME","TELEFONE","TENTATIVA","DATA"));
    while ($resultado = pg_fetch_array($linha) ) {
        $chamado = $resultado['numerochamado'];
        $nome = $resultado['nome'];
        $telefone = $resultado['telefone'];
        $tentativa = $resultado['tentativas'];
        $lastAttempt = $resultado['atualizado'];
        $dataconvertida = date('d/m/Y H:i:s', strtotime($lastAttempt));
        $codigo = $resultado['codigo'];    
        fputcsv($output,array($chamado.";".$nome.";".$telefone.";".$tentativa.";".$dataconvertida));
    }
?>

It is functional, however wanted each data to be in a cell, instead of all being in cell A, there is alternative to this?

  • vc says that the last fputcsv is putting the data in a single cell, in this case the CALLED cell, that?

  • 1

    That, I would like that what was corresponding to call to be in cell A, what was called Cell B, instead of all grouped in cell A as this occurring

  • the name of the fields are separate? or are together tbm?

  • 1

    You are grouping the array with "." this is the problem. fputcsv expects separate fields.

  • 1

    @Wees are together too

  • @Bacco, I also thought it was, but in the first array is already saving in a single cell

  • 1

    @Weessmith that’s why he didn’t set the separator to ';' (default is ',').

Show 2 more comments

1 answer

5


Your mistake is here, you’re concreting everything into one string:

fputcsv($output,array($chamado.";".$nome.";".$telefone.";".$tentativa.";".$dataconvertida));


Correct way:

//                     .--- Array de campos
fputcsv($output, array($chamado, $nome, $telefone, $tentativa, $dataconvertida), ';');
//       '--- handle do arquivo                                      separador ---'

The first field is the Handle, the second the array of fields, the third the separator. Do not forget to put the separator in the titles as well.

Handbook:

https://secure.php.net/manual/en/function.fputcsv.php

  • 1

    In this format, separating only by comma, continues to group everything in cellular A yet.

  • 1

    That’s lack of you set the separator.

  • Ball show guys!

  • I also did not have this knowledge, thank you for learning.

  • 2

    @Weessmith are there for this. But it is important to always read the manual. It often has routine functions with many details that we can’t even imagine. As much experience as I have with PHP, I’m always rereading. (This goes for all the languages I use - knowing how to use is much more important than "memorizing" the function). I recommend that people do the same, regardless of experience.

  • perfect @bacco ,really did not have this knowledge, as it was a common thing that I never did before, did not have this knowledge.

  • Thank you both for your help

  • 1

    @Willianlima for a moment I thought you had disappeared, I was worried not to know if you had solved or not kkk - I was even commenting on the network chat. Having doubts, ask (but always read the manual, it is he who can save you at times when there is no one here to answer) :)

  • @Bacco Come out to lunch dude hahaha It was really careless, I was searching some ways to solve this problem, but it didn’t even cross the head of the manual, serves as learning now haha Thank you!

  • @Bacco I always do this

Show 5 more comments

Browser other questions tagged

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