I want to build a graph in jqchart where i need to get two arrays
Now i want to perform operation as given below.Which is giving error ofcourse.
html
$.ajax(
{
type: "GET",
url: "customer_coverage.php",
data: {id:id},
contentType: "application/json",
dataType: "json",
success: function (data21,data22) {
initChart2(data21,data22);
}
});
function initChart2(data21,data22) {
$('#jqChart2').jqChart({
series: [
{
type: 'column',
title: 'no of days ',
data:data21,
},
{
type: 'column',
title: 'no of days ',
data:data22,
},
]
});
}
heres PHP code
echo json_encode($arr1);
echo json_encode($arr2);
So any one has idea of how to do it?
no need to echo json encode two times....merge the array and send the data.......
echo json_encode(array('result1'=>$arr1,'result2'=>$arr2));
and get data by
initChart2(data.result1,data.result2);
See if you are able to produce two object arrays of json then you can try with this:
var data21,data22;
$.ajax({
type: "GET",
url: "customer_coverage.php",
data: {id:id},
contentType: "application/json",
dataType: "json",
success: function (data) {
$.each(data, function(i, item){
data21 = item.data21;
data22 = item.data22;
});
initChart2(data21,data22);
}
});
and i am supposing if you are able to produce this:
[
{
"data21": {
.........
},
"data22": {
........
}
}
]
You cannot get multiple object like that. For a JSON object, you will need to have single object. So what you can do is, create a wrapper object put those two array inside it.
So basically, your php code will be:
<?php
$arr= array();
$arr['arr1'] = $arr1;
$arr['arr2'] = $arr2;
echo json_encode($arr);
?>
So now you will have single main array and so single JSON object.
On JS side, you will get single data. Little Modification will be
$.ajax(
{
type: "GET",
url: "customer_coverage.php",
data: {id:id},
contentType: "application/json",
dataType: "json",
success: function (data) {
var data21=data['arr1'];
var data22=data['arr2'];
initChart2(data21,data22);
}
});
This should work.
You need to combine both array using array_merge()
.
Example
$response = array();
$response = array_merge($arr1,$arr2);
echo json_encode($response);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With