How to communicate data from my database with chart (Highcharts)?

Asked

Viewed 115 times

0

I am beginner in php, I am trying to supply my graphic with information from my bank.

Esse é o erro que aparece pra mim

Meu código com a lógica de inserção de dados no gráfico

Banco para ajudar melhor no entendimento da pergunta

  • 1

    Ai instead of posting the image of the code you should put in a code block the example. So it is easy to help.

  • @Allan Conde are you using ajax? an alternative is to send the data to your view (which will have js building the graph) via ajax, ai in php vc already leaves everything formatted. Try to var_dump the records that are being returned in the database to see if this part ok

  • @Allan Conde you just tried to fetch instead of fetch_array?

  • @Karen Vicente tried after her comment, but I did not get any results. I am not using Ajax.

  • @Where did you try fetch_array() without the parameter? , do you have this code available somewhere, like github I think it would be easier to help you like this? or provides only this graphic class.php

  • 1

    @Karen Vicente I’ve tried but it didn’t work, https://github.com/AllanConde/graficopesquisa.git

  • @Allan Conde thank you!! I will try to run your code to find the problem :)

Show 2 more comments

1 answer

0


I made some adjustments to your code:

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Highcharts Example</title>

        <style type="text/css">

        </style>
    </head>
    <body>
  <?php

    include 'conexao.php';
    $resTotal = $con->query("SELECT * FROM aluno");
    $cnt = $resTotal->rowCount();

    $resProf1 = $con->query("SELECT * FROM aluno where professor='lobato'");
    $cntProf1 = $resProf1->rowCount();
    $totalProf1 = $cntProf1 * 100 /$cnt;

    $resProf2= $con->query("SELECT * FROM aluno where professor='willys'");
    $cntProf2= $resProf2->rowCount();
    $totalProf2 = $cntProf2 * 100 /$cnt;


    $resProf3= $con->query("SELECT * FROM aluno where professor='allan'");
    $cntProf3 = $resProf3->rowCount();
    $totalProf3 = $cntProf3 * 100 /$cnt;

    while($filaProf1 = $resProf1->fetch(PDO::FETCH_ASSOC)){
      $prof1 = "{ name: '".$filaProf1['professor']."', y:".$totalProf1."},";
    }
    while($filaProf2 = $resProf2->fetch(PDO::FETCH_ASSOC)){
      $prof2 = "{ name: '".$filaProf2['professor']."', y:".$totalProf2."},";
    }
    while($filaProf3 = $resProf3->fetch(PDO::FETCH_ASSOC)){
      $prof3 = "{ name: '".$filaProf3['professor']."', y:".$totalProf3."},";
    }

    var_dump($prof1);
   ?>
<script src="highcharts/code/highcharts.js"></script>
<script src="highcharts/code/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>



        <script type="text/javascript">

Highcharts.chart('container', {
    chart: {
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false,
        type: 'pie'
    },
    title: {
        text: 'Browser market shares January, 2015 to May, 2015'
    },
    tooltip: {
        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
    },
    plotOptions: {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                style: {
                    color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                }
            }
        }
    },
    series: [{
        name: 'Brands',
        colorByPoint: true,
        data: [
          <?php
            echo isset($prof1)? $prof1 : null;
            echo isset($prof2)? $prof2 : null;
            echo isset($prof3)? $prof3 : null;
          ?>
        ]
    }]
});
        </script>
    </body>
</html>

I changed the fetch_array for fetch(PDO::FETCH_ASSOC) I var_dump the result and it seems that this ok.

I also changed the line:

series: [{
        name: 'Brands',
        colorByPoint: true,
        data: [
          <?
            echo

was missing the <?php and added the following validation:

<?php
            echo isset($prof1)? $prof1 : null;
            echo isset($prof2)? $prof2 : null;
            echo isset($prof3)? $prof3 : null;
          ?>

this ternary validates if the variable exists in the scope, because if no result is found for the $prof2 or $prof1 or $prof3 the line that was may cause error in php, trying to use a variable that does not exist, because it is declared only inside while, ie only if you find some record in the query.

I saw here that there is an error of import of the graphics file, I do not know if you are using files of your machine, in case the problem persists send the files of Imports also there we can help you better.

I hope I’ve helped :)

Obs: I considered the following bank:

CREATE TABLE `dbpesquisa`.`aluno` (
  `idaluno` INT NOT NULL AUTO_INCREMENT,
  `nome` VARCHAR(45) NULL,
  `sobrenome` VARCHAR(45) NULL,
  `curso` VARCHAR(45) NULL,
  `professor` VARCHAR(45) NULL,
  PRIMARY KEY (`idaluno`));
  • 1

    our perfect, ran clean thank you very much for the force Karen, now I will be able to increment more data and generate more reports in my research.

Browser other questions tagged

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