I have a logic problem to display the data in an array.
It is the following:
I have a function, which returns me the result of registrations per UF per day. In this step 2 parameters, an array with the dates and an array with the states.
Mounting the parameters to pass to the function.
$uf = array('sp','rj');
$d1 = '2015-09-15';
$d2 = '2015-09-17';
$timestamp1 = strtotime( $d1 );
$timestamp2 = strtotime( $d2 );
while ( $timestamp1 <= $timestamp2 ) {
$data_mod[] = date( 'Y-m-d', $timestamp1 ) . PHP_EOL; // data_mod vira um array com as data entre $d1 e $d2
$timestamp1 += 86400;
}
Function:
function grafico_cadastro_por_dia($conexao,$uf,$data_mod){
$lista = array();
$qtd_dias = count($data_mod);
$qtd_uf = count($uf);
for($i = 0; $i <= $qtd_uf; $i++) {
if( $uf[$i] ) { $where[] = " uf = '{$uf[$i]}'"; } //monto o implode para o array de UF
}
for($i = 0; $i <= $qtd_dias; $i++) {
if( $data_mod[$i] ) { $where2[] = " dataCadastro = '{$data_mod[$i]}'"; } //monto o implode para o array de datas
}
$query = "SELECT dataCadastro,uf,sum(cadastros) FROM tblCadastros where (".implode( ' or ',$where2 ).") AND (".implode( ' or ',$where ).") group by 1,2";
$sql = mysql_query($query,$conexao);
while($row = mysql_fetch_assoc($sql)){
$lista[] = $row;
}
return $lista;
}
On return of the function, I get the following data.
$funcao = grafico_cadastro_por_dia($conexao,$uf,$data_mod);
for($i = 0; $i <= count($funcao); $i++){
echo '<br>';
print_r($funcao[$i]);
}
Resultado:
Array ( [dataCadastro] => 2015-09-15 [uf] => SP [sum(cadastros)] => 36 )
Array ( [dataCadastro] => 2015-09-15 [uf] => RJ [sum(cadastros)] => 9 )
Array ( [dataCadastro] => 2015-09-16 [uf] => SP [sum(cadastros)] => 19 )
Array ( [dataCadastro] => 2015-09-16 [uf] => RJ [sum(cadastros)] => 8 )
Array ( [dataCadastro] => 2015-09-17 [uf] => SP [sum(cadastros)] => 14 )
Array ( [dataCadastro] => 2015-09-17 [uf] => rj [sum(cadastros)] => 2 )
As you can see, the function returns 2 times the date because it has more than one state to display the value.
If I pass 3 states in the array, it would display 3 times the same date.
So far so good.
The problem starts now, since I'm going to put those results on a graph.
And I can not print the results in the order the chart asks for.
You would have to display in that order:
[$data, $total "uf SP" , $total "uf RJ"]
['2015-09-15', 36 , 9],// 36 é o valor de cadastro de SP no dia 15 e 9 o valor de cadastros de RJ no dia 15
['2015-09-16', 19 , 8],
and so on.
But I have already sketched everything I can imagine, but I could not display the data that way.
Can anyone help me?