High charts in php and mysql

Asked

Viewed 260 times

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);

Return: inserir a descrição da imagem aqui

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:

inserir a descrição da imagem aqui

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:

inserir a descrição da imagem aqui

  • Some error is pointed in the console?

  • @Darlei Fernando Zillmer does not point out any error in the console

  • @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 do var_dumpor print_r and printed null or empty.

  • 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 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?

  • 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?

Show 2 more comments

1 answer

1


The error is in the second parameter of array_column, you need to define a column of values to be returned, which in the case exists in the array return of the query. How do you do:

... COUNT(Descricao) AS Tarefa ...

Its associative key is Task, soon:

...
$viewer = json_encode(array_column($viewer, 'Tarefa'),JSON_NUMERIC_CHECK);
...
$click = json_encode(array_column($click, 'Tarefa'),JSON_NUMERIC_CHECK); 
...

Reference: Documentation array_column

  • I can just ask you a little question in the chat room?

  • Of course, you can

Browser other questions tagged

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