Fputcsv insere apenas uma linha
Olá pessoal,
Estou com um problema para escrever um arquivo csv utilizando a função FPUTCSV.
<?php
$queryList = "SELECT
wzap.id as id,
wzap.mobile as mobile,
wzap.ddi as ddi,
wzap.pushname as pushname,
wzap.photo as photo,
wzap.dlr_date as dataDlr,
case
when wzap.status = '0' then 'Pendente'
when wzap.status = '1' then 'Enviado'
when wzap.status = '2' then 'Entregue'
when wzap.status = '3' then 'Não entregue'
when wzap.status = '4' then 'Rejeitado'
when wzap.status = '5' then 'Expirou'
when wzap.status = '6' then 'Agendado'
when wzap.status = '7' then 'Falha'
end as status,
case
when wzap.status = '1' and wzap.substatus = '1' then 'Processado'
when wzap.status = '2' and wzap.substatus = '1' then 'Ok'
when wzap.status = '6' and wzap.substatus = '1' then 'Processado'
when wzap.status = '7' and wzap.substatus = '1' then 'Sem saldo'
when wzap.status = '7' and wzap.substatus = '2' then 'Erro no processamento'
end as descricao,
wzap.cost as tarifa,
wzap.currency as moeda,
wzap.text as texto,
w.country as pais,
channel.name as canalNome
FROM wzap_mt as wzap
INNER JOIN worldwide as w ON w.ddi = wzap.ddi
LEFT JOIN wzap_channel as channel ON channel.id = wzap.channel_id
WHERE wzap.user_id='{$userID}' and wzap.channel_id {$canalID} and wzap.dlr_date BETWEEN '".$inicio." 00:00:00' and '".$final." 23:59:59'
UNION ALL
SELECT
wzap.id as id,
wzap.mobile as mobile,
wzap.ddi as ddi,
null as pushname,
null as photo,
wzap.dlr_date as dataDlr,
case
when wzap.status = '0' then 'Pendente'
when wzap.status = '1' then 'Enviado'
when wzap.status = '2' then 'Entregue'
when wzap.status = '3' then 'Não entregue'
when wzap.status = '4' then 'Rejeitado'
when wzap.status = '5' then 'Expirou'
when wzap.status = '6' then 'Agendado'
when wzap.status = '7' then 'Falha'
end as status,
case
when wzap.status = '1' and wzap.substatus = '1' then 'Processado'
when wzap.status = '2' and wzap.substatus = '1' then 'Ok'
when wzap.status = '6' and wzap.substatus = '1' then 'Processado'
when wzap.status = '7' and wzap.substatus = '1' then 'Sem saldo'
when wzap.status = '7' and wzap.substatus = '2' then 'Erro no processamento'
end as descricao,
wzap.cost as tarifa,
wzap.currency as moeda,
wzap.text as texto,
w.country as pais,
channel.name as canalNome
FROM wzap_mt_temp as wzap
INNER JOIN worldwide as w ON w.ddi = wzap.ddi
LEFT JOIN wzap_channel as channel ON channel.id = wzap.channel_id
WHERE wzap.user_id='{$userID}' and wzap.channel_id {$canalID} and wzap.dlr_date BETWEEN '".$inicio." 00:00:00' and '".$final." 23:59:59'
UNION ALL
SELECT
wzap.id as id,
wzap.mobile as mobile,
wzap.ddi as ddi,
wzap.pushname as pushname,
wzap.photo as photo,
wzap.dlr_date as dataDlr,
case
when wzap.status = '8' then 'Recebido'
end as status,
case
when wzap.status = '8' and wzap.substatus = '1' then 'Resposta recebida'
end as descricao,
wzap.cost as tarifa,
wzap.currency as moeda,
wzap.text as texto,
w.country as pais,
channel.name as canalNome
FROM wzap_mo as wzap
INNER JOIN worldwide as w ON w.ddi = wzap.ddi
LEFT JOIN wzap_channel as channel ON channel.id = wzap.channel_id
WHERE wzap.user_id='{$userID}' and wzap.channel_id {$canalID} and wzap.dlr_date BETWEEN '".$inicio." 00:00:00' and '".$final." 23:59:59'
ORDER BY dataDlr DESC";
$sqlList = mysqli_query($connect_sql,$queryList);
while($data = mysqli_fetch_assoc($sqlList)){
$stats[] = [
"id" => $data['id'],
"mobile" => $data['mobile'],
"pais" => $data['pais'],
"nome" => $data['pushname'],
"canal" => $data['canalNome'],
"dataDlr" => date("d/m/y H:i:sa", strtotime($data['dataDlr'])),
"status" => $data['status'],
"desc" => $data['descricao'],
"tarifa" => $data['tarifa'],
"moeda" => $data['moeda'],
"texto" => $data['texto']
];
}
$filename = sha1($userID.date("Y-m-d").rand(1,100000));
$header = ["ID","Número","País","Nome","Canal","DataStatus","Status","Descrição","Tarifa","Moeda","Texto"];
$file = fopen(BASEPATH."/export/{$filename}.csv", 'w');
//TRECHO PROBLEMATICO ABAIXO
fputs($file, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
fputcsv($file,$header,",");
foreach ($stats as $line) {
fputcsv($file, $line,",");
}//TRECHO PROBLEMATICO ACIMA
$fileSize = filesize(BASEPATH."/export/{$filename}.csv");
?>
Meu código traz uma lista de uma consulta do meu banco de dados que fica salva no array $stats.
Ao fazer o debug do array $stats vejo que os dados são recebidos perfeitamente neste modelo
Array
(
[id] => 25
[mobile] => 5511945658451
[pais] => Brasil
[nome] => Guilherme
[canal] => guilherme business
[dataDlr] => 19/11/18 11:51:36am
[status] => Entregue
[desc] => Ok
[tarifa] => 0.1
[moeda] => BRL
[texto] => envio via portal - teste de agenda - 11h50m
)Array
(
[id] => 28
[mobile] => 5511950917200
[pais] => Brasil
[nome] => Comercial
[canal] => guilherme business
[dataDlr] => 19/11/18 11:51:27am
[status] => Entregue
[desc] => Ok
[tarifa] => 0.1
[moeda] => BRL
[texto] => envio via portal - teste de agenda - 11h50m
)
Ao fazer o debug também do foreach da variavel $line eu também tenho todos os dados ou seja, minha busca no bd está ok e meu foreach também.
Assim deveria ser escrito meu CSV onde cada indice do array $stats é uma linha separados por virgula.
Porem meu csv quando visualizo, traz apenas uma linha no seguinte formato
ID,Número,País,Nome,Canal,DataStatus,Status,Descrição,Tarifa,Moeda,Texto
27,5511945658451,Brasil,Guilherme,"guilherme business","21/11/18 13:16:41pm",Entregue,Ok,0.1,BRL,"teste foto perfil
Já tentei de tudo e não consigo identificar o porque meu fputcsv está escrevendo apenas uma linha do meu array.
Vendo este código alguem conseguiria me dar uma luz/orientação?
Discussão (1)
Carregando comentários...