Manipulating Array in PHP

Asked

Viewed 531 times

3

I’m creating a PHP that creates Graphics using Google Charts, but I can’t supply the function with all the data in the array. When I print the function, instead of printing all the states of the array, it prints only the first one. Go on like I’m doing:

$query = "SELECT estado, count(*) as quantidade FROM tb_participacoes where id_campanha = ". $id_campanha ." GROUP BY estado";

$mostrar = mysql_query($query);

while ($produto = mysql_fetch_array($mostrar)) {
$valores = $produto['estado'];
$referencia = $produto['quantidade'];
}
$grafico = geraGrafico(500, 200, array($valores), array($referencia)) ;
  • 1

    Testing using [] after values, like this: $valores[] = $produto['estado'];

2 answers

3

In the while that has in your code you are always overriding the value of $valores and $referencia with each iteration, I think you want to use these variables as arrays.

In this case you should use it like this (note the [] extra in the code):

while ($produto = mysql_fetch_array($mostrar)) {
    $valores[] = $produto['estado'];
    $referencia[] = $produto['quantidade'];
}

0

As Sergio mentioned, use [] in the values and reference variables. Then, when calling the graph, pass the array:

$grafico = geraGrafico(500, 200, $valores, $referencia);
  • Why the negative vote?

  • ?? I did not vote against. So far, I have not voted against stackoverflow

  • 1

    My question was not for you :) It was to whom voted your negative answer, I would like to know why.

  • @Sergio Ah ok, I got it wrong. Really, I didn’t know why :P

Browser other questions tagged

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