I've read many many examples from here and for some reason I just cant get them to work.
I have taken the example from here:
PHP MySQL Google Chart JSON - Complete Example
and am using the PHP-MySQLi-JSON-Google Chart Example
For some reason the data was just not pulled from the mysql database using the foreach method. I have now changed this to a while loop using fetch_assoc and it has pulled the data and created the correct json format.
When I load the page though I just get a blank page.
I now have no idea as to why its not working.
Here's some info which might help: php 5.3.17 OpenSuse 12.3
I've 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 a 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>
I changed it to this:
I even more confused now as to why all the examples listed on SO are given and they say they work.