Reputation: 55
I am working on a script that is getting data from an database via PHP and then passes it on to another script which is using the data as an input to high charts (bar drilldown). The data is sent using an JSON array. I have two files looking like following:
On the server side:
<?php
header("content-type: application/json");
$array = array(
"y" => array(44.11),
"color" => array( "colors" => array(0)),
"drilldown" => array(
"name" => array("MSIE versions"),
"categories" => array("MSIE 6.0","MSIE 7.0","MSIE 8.0", "MSIE 9.0"),
"data" => array(1,2,3,5,6),
"color" => array("colors" => array(0))
)
);
echo $_GET['callback']. '('. json_encode($array) . ')';
?>
And on the client side:
$(document).ready(function() {
var colors = Highcharts.getOptions().colors,
categories = ['MSIE', 'Firefox', 'Chrome', 'Safari', 'Opera'],
name = 'Browser brands',
data = [{}];
var url = "php/live-server-data.php?callback=?";
$.getJSON(url, function(input) {
data = input
});
The format of the input that High charts needs (and what i am trying to recreate) is:
data = [{
y: 55.11,
color: colors[0],
drilldown: {
name: 'MSIE versions',
categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],
data: [10.85, 7.35, 33.06, 2.81],
color: colors[0]
}}]
});
Ii don't get it to work, can any one help me to see what error I have done? Greatful for any help. I am stuck
br.
Upvotes: 0
Views: 249
Reputation: 2873
I think you have to replace this:
echo $_GET['callback']. '('. json_encode($array) . ')';
With
echo json_encode($array);
If you need to join the "callback" array with the $array, you have to do programmatically and not treating like strings....
Hope this helps.
Upvotes: 1
Reputation: 706
If the expected format is: [{...}]
then isn't your example above missing the outermost array? The PHP data returned currently is {...}
.
This could be resolved in the PHP with:
echo $_GET['callback']. '(['. json_encode($array) . '])';
Or in the JS with:
data = [input]
Upvotes: 1