Using google charts with php / mysqli - can't get it to work

I have read many examples from here and for some reason I just cannot get them to work.

I gave an example from here:

PHP MySQL Google Chart JSON - Complete Example

and I am using PHP-MySQLi-JSON-Google Chart example

For some reason, the data was simply not being fetched from the mysql database using the foreach method. Now I changed this to a while loop using fetch_assoc and it pulled in the data and generated the correct json format.

When I load the page, I just get a blank page.

Now I don't know why it doesn't work.

Here is some information that might help: php 5.3.17 OpenSuse 12.3

I have checked the apache logs and there are no errors. Any ideas what else I can do to figure this out?

jsonTable:
{
"cols":[
    {"label":"Weekly Task","type":"string"},
    {"label":"Percentage","type":"number"}
    ],
"rows":[
    {"c":[{"v":"running"},{"v":30}]},
    {"c":[{"v":"jorunning"},{"v":30}]},
    {"c":[{"v":"job"},{"v":20}]},
    {"c":[{"v":"sleeping"},{"v":40}]},
    {"c":[{"v":"exercise"},{"v":50}]},
    {"c":[{"v":"resting"},{"v":30}]}
]
}

      

Here is my code:

 <?php

 $DB_NAME = 'chart';
 $DB_HOST = 'localhost';
 $DB_USER = 'test';
 $DB_PASS = '123456';

$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}
$query = "select * from googlechart";

if ($result = $mysqli->query($query)) {
    {
    $rows = array();
    $table = array();
    $table['cols'] = array(
            array('label' => 'Weekly Task', 'type' => 'string'),
            array('label' => 'Percentage', 'type' => 'number')
    );

    while ($row = $result->fetch_assoc()) {
            $temp = array();
            $temp[] = array('v' => (string) $row['weekly_activity']);
            $temp[] = array('v' => (int) $row['percentage']);
            $rows[] = array('c' => $temp);
            }
   }
 }

$table['rows'] = $rows;
$jsonTable = json_encode($table);
?>
<html>
<head>
<!--Load the Ajax API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
 <script type="text/javascript">
 // Load the Visualization API and the piechart package.

 google.load('visualization', '1', {'packages':['corechart']});

 // Set a callback to run when the Google Visualization API is loaded.
 google.setOnLoadCallback(drawChart);

 function drawChart() {

  // Create our data table out of JSON data loaded from server.
  var data = new google.visualization.DataTable(<?=$jsonTable?>);
  var options = {
       title: 'My Weekly Plan',
      is3D: 'true',
      width: 800,
    height: 600
    };
  // Instantiate and draw our chart, passing in some options.
  // Do not forget to check your div ID
  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  chart.draw(data, options);
}
</script>
</head>

<body>
<!--this is the div that will hold the pie chart-->
<div id="chart_div"></div>
    <?php echo $jsonTable; ?>
 </body>
 </html> 

      

Here is the source from the loaded page:

<html>
<head>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">   </script>
<script type="text/javascript">

// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

function drawChart() {

  // Create our data table out of JSON data loaded from server.
  var data = new google.visualization.DataTable(<?=$jsonTable?>);
  var options = {
       title: 'My Weekly Plan',
      is3D: 'true',
      width: 800,
      height: 600
    };
  // Instantiate and draw our chart, passing in some options.
  // Do not forget to check your div ID
  var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
  chart.draw(data, options);
}
</script>
</head>

<body>
<!--this is the div that will hold the pie chart-->
<div id="chart_div"></div>

</body>
</html>

      

0


source to share


1 answer


I changed it to this:

 <?php echo $jsonTable; ?>

      



I am even more confused as to why all the examples listed on SO are given and say they work.

+1


source







All Articles