To answer your question I had to create other tables that were not informed to get nome_representante
and nome_cliente
. And for testing I've created a few miscellaneous records, see:
Asanoption,Iusedtheinputvalueatthebeginningoftheyearandattheendoftheyeartobeanalyzed,becauseyoumightwanttomakeafilter.Seethelinebelow:
$anoInicio="2017";
$anoFim = "2018";
Finally, I created the code based on yours. Notice that select
is sorting, first by month, then by customer code, and then by year. This way you will get the expected result.
$anoInicio = "2017";
$anoFim = "2018";
$query = mysqli_query($conn, "SELECT ano, mes,
(
SELECT nome_cliente FROM cliente
WHERE cliente.codigo_cliente = clientes_vendas.codigo_cliente
) AS 'nome_cliente',
(
SELECT nome_representante FROM representante
WHERE representante.codigo_representante = clientes_vendas.codigo_representante
GROUP BY representante.codigo_representante
) AS 'nome_representante',
SUM(valor) AS 'total' FROM clientes_vendas
WHERE ano >= '$anoInicio' AND ano <= '$anoFim'
GROUP BY mes, codigo_cliente, ano ORDER BY codigo_cliente, mes, ano ASC"
);
$arraDados = array();
$strTmpCliente = "";
$mes = array('', 'Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'); // formatador dos meses
$x = 1;
while($dados = mysqli_fetch_assoc($query)){
if($strTmpCliente != $dados['nome_cliente']){
$strTmpCliente = $dados['nome_cliente'];
$arraDados[$strTmpCliente] = array();
$arraDados[$strTmpCliente]['nome'] = $dados['nome_cliente'];
$arraDados[$strTmpCliente]['representante'] = $dados['nome_representante'];
}
$strMesAno = $mes[$dados['mes']]."_".$dados['ano'];
$arraDados[$strTmpCliente][$strMesAno] = number_format($dados['total'],2,",",".");
}
$header = "Cliente;Representante;";
$mesAtual = 1;
$anoAtual = (int)$anoInicio;
$arrayDatas = array();
for($x = 0; $x < 24; $x++){
$data = $mes[$mesAtual]."_".$anoAtual;
$arrayDatas[] = $data;
$header .= str_replace("_", "'",$data).";";
$alteraMes = $x % 2 == 0 ? false : true;
if($alteraMes){
$mesAtual++;
$anoAtual = $anoInicio;
continue;
}
$anoAtual = $anoFim;
}
$header = substr($header, 0, -1);
$header .= "\n";
$strDados = array();
foreach($arraDados as $cliente ){
$strTmp = $cliente['nome'].";".$cliente['representante'].";";
for($i = 0; $i < count($arrayDatas); $i++){
$data = $arrayDatas[$i];
if(isset($cliente[$data])){
$strTmp .= $cliente[$data].";";
continue;
}
$strTmp .= "-;";
}
$strTmp = substr($strTmp, 0, -1);
$strTmp .= "\n";
$strDados[] = $strTmp;
}
$csv = "arquivo.csv";
// escreve o cabeçalho
$fp1 = fopen($csv, 'w');
fwrite($fp1, $header);
fclose($fp1);
// escreve o resultado
$fp2 = fopen($csv, 'a');
foreach ($strDados as $file) {
fwrite($fp2, $file);
}
fclose($fp2);
The generated file looks like this:
EDIT
Inoticedthatin image that you posted that a few months went without sales . I went to do the test in my code and, as I imagined, it made an error. For the values were filled in sequentially.
I've changed the code and is now working properly. I was able to enter the number_format
you are using. The result was this:
EDIT2
Changethispartthatwillwork:
if($strTmpCliente!=$dados['nome_cliente']){$strTmpCliente=$dados['nome_cliente'];$arraDados[$strTmpCliente]=array();$arraDados[$strTmpCliente]['nome']=$dados['nome_cliente'];$arraDados[$strTmpCliente]['representante']=$dados['nome_representante'];}//--->oerroestavaaqui$dadosMes=(int)$dados['mes'];//aquieletransformaeminteiro$strMesAno=$mes[$dadosMes]."_".$dados['ano']; // aqui ele grava a strinmg corretamente
$arraDados[$strTmpCliente][$strMesAno] = number_format($dados['total'],2,",",".");