1
I wonder if you could help me with the following situation:
I have a column called event type , in it after completing a form will be inserted some of the 4 items: Collision, PT, THEFT, Theft.
I’m wanting to make a query and know how many times each of the items appears in the column and display them in graph using chartjs.
Follows code below:
public function contar2()
{
try {
$query = $this->bd->prepare("SELECT tipo_evento, COUNT(tipo_evento) AS Quantidade FROM vistorias GROUP BY tipo_evento HAVING COUNT(tipo_evento) > 1 ORDER BY COUNT(tipo_evento) DESC");
$contar_dados = array($_GET['tipo_evento']);
$query->execute(array($contar_dados));
$conta = $query->rowCount();
if ($conta > 0) {
$query->fetch(PDO::FETCH_ASSOC);
}
} catch (PDOException $e) {
die($e->getMessage());
}
return $query->fetchAll();
}
On the page where I want to display the items, I am calling the function as follows:
$contar2 = $acoes->contar2();
$furto = $contar2[2]['tipo_evento'];
$furto2 = $contar[2]['Quantidade'];
I put the command below on the page and returned me the array as follows:
echo '<pre>';
print_r($cliente);
echo '</pre>';
[
{
0: {
0: "Colisão",
1: "4",
tipo_evento: "Colisão",
Quantidade: "4"
},
1: {
0: "PT",
1: "3",
tipo_evento: "PT",
Quantidade: "3"
},
2: {
0: "Furto",
1: "3",
tipo_evento: "Furto",
Quantidade: "3"
},
3: {
0: "Opcionais",
1: "2",
tipo_evento: "Opcionais",
Quantidade: "2"
}
},
"</pre>",
You could take this information from the array and insert it into the chartjs chart ?
I can even catch them if I do:
$furto = $contar2[2]['Quantidade'];
echo "Meu Array possui: ".$furto2.' elementos';
This way, it returns me how many times it was displayed, but I can not put inside the chartjs...
Thanks in advance !
Oops, I think the easiest way is to turn this into json and move it to your msm frontend, either using ajax requests or creating javascript object with php when displaying your page type
<script> var obj = <?php echo json_encode($seu_array) ?> </script>
– andre_luiss
@andre_luiss - Yeah, I’m sorry the ignorance, but I didn’t understand how I could call the values inside the graph, I could exemplify a little more for kindness
– Guilherme Mayrink
Because it is chartJS you have to send an array to it, but it is up to you to format this array to fit the chart and make sense. If Voce has
$array = ['fruta' => 'banana']
, usingjson_encode
the output of the array would be a string{"fruta":"banana"}
, so you are already able to manipulate via javascript– andre_luiss