1
I am trying to create dynamic highchart charts in php and mysql. I started by creating the database queries:
$query = "SELECT Descricao, COUNT(Descricao) AS Tarefa FROM raddb.Tarefa AS A LEFT OUTER JOIN raddb.TipoTarefa AS B ON B.Id = A.Tarefa
GROUP BY Tarefa ORDER BY Descricao;";
$viewer = mysqli_query($conn,$query);
$viewer = mysqli_fetch_all($viewer,MYSQLI_ASSOC);
$viewer = json_encode(array_column($viewer, 'count'),JSON_NUMERIC_CHECK);
Second consultation:
$sql = "SELECT Colaborador, COUNT(Descricao) AS Tarefa FROM raddb.Tarefa AS A LEFT OUTER JOIN raddb.TipoTarefa AS B ON B.Id = A.Tarefa
GROUP BY Tarefa, Colaborador ORDER BY Descricao;";
$click = mysqli_query($conn,$sql);
$click = mysqli_fetch_all($click,MYSQLI_ASSOC);
$click = json_encode(array_column($click, 'count'),JSON_NUMERIC_CHECK);
Return:
Then how do I create the chart:
$(function () {
var data_click = <?php echo $click; ?>;
var data_viewer = <?php echo $viewer; ?>;
$('#container').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Yearly Website Ratio'
},
xAxis: {
categories: ['2013','2014','2015', '2016']
},
yAxis: {
title: {
text: 'Rate'
}
},
series: [{
name: 'Click',
data: data_click
}, {
name: 'View',
data: data_viewer
}]
});
});
</script>
<div class="container">
<br/>
<h2 class="text-center">Highcharts php mysql json example</h2>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>
<div class="panel-body">
<div id="container"></div>
</div>
</div>
</div>
</div>
</div>
But when I open the page with the graphic, it is blank as shown in the image:
Some error is pointed in the console?
– Darlei Fernando Zillmer
@Darlei Fernando Zillmer does not point out any error in the console
– Bruno
@Darlei Fernando Zillmer I think the problem is here,
$click = json_encode(array_column($click, 'count'),JSON_NUMERIC_CHECK);
and$viewer = json_encode(array_column($viewer, 'count'),JSON_NUMERIC_CHECK);
why dovar_dump
orprint_r
and printed null or empty.– Bruno
Really, there should be the problem, if your variables have nothing, the chart will not show anything. Revise the functions of the database, debug well in hand even with var_dump in the json_encode and array_column functions. Enabling error_reporting is also a good thing if it’s not enabled.
– Darlei Fernando Zillmer
@Darlei Fernando Zillmer but the queries are correct. The problem I’m having and I’m not getting it, is how I send the data of the queries or just a querie to the graph, can help?
– Bruno
The passage of values is not wrong. The code is sequential, if php comes before JS, the variables will be set. You tested queries directly in the database or in mysqli_query?
– Darlei Fernando Zillmer
Let’s go continue this discussion in chat.
– Bruno