Re: Format data to JSON

83 views
Skip to first unread message

asgallant

unread,
Dec 30, 2012, 9:39:05 PM12/30/12
to google-visua...@googlegroups.com
I'm not sure why that code isn't throwing an error when you try to create the DataTable object (because the JSON isn't a valid DataTable structure).  The cause is you aren't resetting $temp with every iteration of the while loop (so after 1 iteration, it has two columns of data, after 2 iterations, it has 4 columns of data, and so on).  Your while loop should look like this:

while($r = mysql_fetch_assoc($result)) 
{
$temp = array(); // this creates an empty array at the start of each iteration
$temp[] = array('v' => (string) $r['datum']);
$temp[] = array('v' => (int) $r['opbrengst']);
$rows[] = array('c' => $temp);
}

On Sunday, December 30, 2012 6:01:26 PM UTC-5, GalaxyBounce wrote:
Hello,

i'm having a problem with getting data to the JSON format.
First i read the data from mysql an put it in an array, 
then i put it in an JSONTable with JSON_ENCODE. But i only get the first parameter of my mysql Table



Here is my code : 
<?php

$q=$_GET["q"];

$con = mysql_connect("xxxxxxxxx","xxxxxx","xxxxxxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//DB selecteren
mysql_select_db("emOs", $con);

//data vn de db krijgen
$result = mysql_query("SELECT datum, opbrengst FROM `dagenvandeweek`;") or die(mysql_error());
$rows = array();
$table = array();

$table['cols']=array(
array('label'=>'datum', 'type'=> 'string'),
array('label'=>'opbrengst', 'type'=> 'number')
);

while($r = mysql_fetch_assoc($result)) 
{
$temp[] = array('v' => (string) $r['datum']);
$temp[] = array('v' => (int) $r['opbrengst']);
$rows[] = array('c' => $temp);
}

$table['rows'] = $rows;

$jsonTable = json_encode($table);

echo $jsonTable;

/*echo '<h1> TABLE </h1>';
displayArray($table);
echo '<h1> TEMP </h1>';
displayArray($temp);
echo '<h1> ROWS </h1>';
displayArray($rows);*/

function displayArray($val)
{
echo "<pre>";
print_r($val);
echo "</pre>";
return;
}

mysql_close($con);
?>

<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':['columnchart']});

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

      // Instantiate and draw our chart, passing in some options.
      //do not forget to check ur div ID
      var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
      chart.draw(data, {width: 400, height: 240});
    }

    </script>
  </head>

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

Reply all
Reply to author
Forward
0 new messages