I have a problem that I have not been able to resolve for more than a week.
I want to do a report of entries for a particular site.
In the report, I want to list the Cities and their total of registrations.
Previously, I would make a query without using a method, only the mysql_query()
function of PHP where I would capture the data in the MySQL database would list the records with the while
loop on the screen. However, I want to improve this, as this system requires me to use a method again and again, I want to adapt it using a method that serves as an instrument for reusing the code.
However, I'm not able to make the result that is within while(...)
appear correctly in my method.
The structure of the code I'm breaking the head:
$uf = $_POST['uf'] //aqui, $uf recebe 1 ou mais estados vai post
$x = count($uf);
for($i = 0 ; $i < $x; $i++){
$funcao = cad_por_uf($uf[$i],$dataInicial,$dataFinal); // aqui ,passo o valor do return da funcao para a variavel $funcao;
echo $funcao[0].'-'.$funcao[1];
}
function cad_por_uf($uf,$dataInicial,$dataFinal){
$sql = mysql_query("SELECT sum(cadastros),uf,cidade FROM tblCadastros where data >= '$dataInicial' and data <= '$dataFinal' and uf = '$uf' and deletada = 0 group by (cidade)");
while($row = mysql_fetch_array($sql)){
$cidade = $row['cidade'];
$cadastros= $row['sum(cadastros)'];
return array($cidade,$cadastros);
}
}
Displaying the $funcao[0]
variable should list all cities in the state. But, it's just returning me 1 result. How could you solve this problem?