Export fopen without ""

Asked

Viewed 43 times

0

I’m using the fopen , to export the return of a select, it is exporting correctly, more in certain fields this appearing "" as I remove the "" of export, leaving only the ,

because it’s coming out as follows: "rice","sweet potato"

I need you to leave: rice, sweet potato...

  <?php 
  require('data/conexao.php');

  $periodo = $_GET['folheto'];

  $data = date("Ymd");
  $hora = date("His");
  $nomeExport = "exportacao-".$data;
  $paginaAtual = "";
  $limite = 24;
  $conta = 0;
  $export = $conn->prepare("SELECT 
    f.prod_folheto_produto AS produto,
    f.prod_folheto_marca   AS marca,
    f.prod_folheto_tipo1   AS descricao,
    f.prod_folheto_tipo2   AS subdescricao,
    SUBSTRING_INDEX(f.prod_folheto_preco,'.',1) AS preco,
    SUBSTRING_INDEX(f.prod_folheto_preco,'.',-1) AS centavos,
    REPLACE(f.prod_folheto_gramatura,',','.') AS gramatura,  
    (CASE 
    WHEN f.prod_folheto_pais = 0 THEN 'BRA' ELSE   p.iso3
    END) AS pais,
    prod_folheto_pagina AS pagina
    FROM 
    produto_folheto AS f
    LEFT JOIN cep_paises AS p
    ON p.numcode = f.prod_folheto_pais
    WHERE
    prod_folheto_periodo = '$periodo'
    ORDER BY prod_folheto_pagina ASC");
  $export->execute(array());


  $paginaAtual = "";
  $limite = 24;
  $conta = 0;

  $novoArr = array();

  while($dados = $export->fetch()){

    if($paginaAtual ==""){ 

      $paginaAtual = $dados["pagina"];

      $conta = 0;

    }elseif($paginaAtual != $dados['pagina']){

     while($conta < $limite){ 

       $novoArr[] = "-,-,-,-,-,-,-";

       $conta++;
     }

     $paginaAtual = $dados["pagina"];

     $conta = 0;
   }
   $conta++;

   $novoArr[] = 
   $dados["produto"]      .","
   .$dados["marca"]        .","
   .$dados["descricao"]    .","
   .$dados["subdescricao"] .","
   .$dados["preco"]        .","
   .$dados["centavos"]     .","
   .$dados["gramatura"]    .",";
 }
 while($conta < $limite){

  $novoArr[] = "-,-,-,-,-,-,-";

  $conta++;


}

$file = fopen("export/$nomeExport.csv","w");

fprintf($file, chr(0xEF).chr(0xBB).chr(0xBF));

fputcsv($file, array('produto','marca','descricao','subdescricao','preco','centavos','gramatura'));

foreach ($novoArr as $line)
{
  fputcsv($file,explode(',',$line));
}

fclose($file); 

?>

What I’ve noticed is that "" appear when there are two words together, example: "batata doce"

1 answer

1


fputcsv

(PHP 5 >= 5.1.0, PHP 7)

fputcsv - Format the line as CSV and write it to a file pointer

Parameters

  • Handle: The file pointer needs to be valid, and point to a file opened by fopen() or fsockopen() (and not yet closed by fclose()).

  • Fields: A array of values.

  • delimite: The optional parameter delimite sets the delimiter character (only one character).

  • Enclosure: The optional parameter Enclosure configures the character that surrounds the data (only one character).

  • escape_char: The optional parameter escape_char sets the escape character (only one character).

Your job would look like this:

fputcsv($file,explode(',',$line), ",", ' ');

or so:

fputcsv($file,explode(',',$line), ",", chr(32));

reference

php.net/fputcsv

Browser other questions tagged

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