0

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>
3
  • Completely sure it is not the copy/paste error <?=$jsonTable?> that might should be <?echo $jsonTable;?> instead ?? Commented Sep 12, 2013 at 17:24
  • Open the page in a browser, view the source, and post it. Commented Sep 12, 2013 at 18:44
  • @davidkonrad - nope, tried that. Didnt work. Source added. Commented Sep 13, 2013 at 17:54

1 Answer 1

1

I changed it to this:

 <?php echo $jsonTable; ?>

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

Sign up to request clarification or add additional context in comments.

1 Comment

Gr8! Think I was pretty close :) You should consider accepting your own answer, so it might can help other people in the future (as they can see that this was a working solution)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.