我有一种情况,我在 php 文件“A”中有一个下拉列表。当我从该下拉列表中选择一个值时,会进行 ajax 调用并触发不同的 php 文件 B。在那个文件中,我做 db fetch,并形成两个 json 对象。这两个 json 对象是我绘制 2 个不同的数据表和 2 个不同的图表所必需的。
当我在“B”中回显一个 json 对象时,我将其作为对“A”中 ajax 调用的响应
如果我在 B 中回显两个 json 对象,我什至无法得到响应。
对于单个 json 对象响应,我可以绘制数据表并可以使用 javascript 操作 json 对象并希望绘制图表
请告知如何处理这种情况 文件 B 中的代码
$json = json_encode($tot_categ);
$json_percent = json_encode($tot_que_percent);
$cols_percent = array(
array( 'id' => 't', 'label' => 'Title', 'type' => 'string'),
array( 'id' => 'n', 'label' => 'Neutral(+) ', 'type' => 'string'),
array('id' => 'a', 'label' => 'Agree', 'type' => 'string'),
array('id' => 'ne', 'label' => 'Neutral(-)', 'type' => 'string'),
array('id' => 'd', 'label' => 'Disagree', 'type' => 'string'),
);
$jcols_percent = json_encode($cols_percent);
//JSON format accepted by Google tables
$r_percent= "{cols:".$jcols_percent.','."rows:".$json_percent."}";
//echo (JSON.stringify($r_percent));
// echo $r_percent;
$cols = array(
array( 'id' => 't', 'label' => 'Title', 'type' => 'string'),
array( 'id' => 'l', 'label' => 'Avg ', 'type' => 'string'),
array('id' => 'lb', 'label' => 'High', 'type' => 'string'),
array('id' => 'lo', 'label' => 'Low', 'type' => 'string')
);
$jcols = json_encode($cols);
//JSON format accepted by Google tables
$r = "{cols:".$jcols.','."rows:".$json."}";
//echo(json_decode($r));
echo $r;
$r 和 $r_percent 是我的对象
回显时的 $r_percent 给出
{cols:[{"id":"t","label":"Title","type":"string"},{"id":"n","label":"Neutral(+) ","type":"string"},{"id":"a","label":"Agree","type":"string"},{"id":"ne","label":"Neutral(-)","type":"string"},{"id":"d","label":"Disagree","type":"string"}],rows:[{"c":[{"v":"165q"},{"v":0},{"v":0},{"v":0},{"v":100}]},{"c":[{"v":"160q"},{"v":0},{"v":0},{"v":0},{"v":6}]},{"c":[{"v":"161q"},{"v":0},{"v":0},{"v":0},{"v":10}]},{"c":[{"v":"162q"},{"v":7},{"v":0},{"v":7},{"v":0}]},{"c":[{"v":"163q"},{"v":0},{"v":25},{"v":0},{"v":0}]},{"c":[{"v":"164q"},{"v":0},{"v":100},{"v":0},{"v":0}]}]}
$r 回显时给出
{cols:[{"id":"t","label":"Title","type":"string"},{"id":"l","label":"Avg ","type":"string"},{"id":"lb","label":"High","type":"string"},{"id":"lo","label":"Low","type":"string"}],rows:[{"c":[{"v":"165q"},{"v":1.3333333333333},{"v":"2"},{"v":"1"}]},{"c":[{"v":"160q"},{"v":6},{"v":"10"},{"v":"1"}]},{"c":[{"v":"161q"},{"v":6.6666666666667},{"v":"9"},{"v":"2"}]},{"c":[{"v":"162q"},{"v":7},{"v":"9"},{"v":"3"}]},{"c":[{"v":"163q"},{"v":8},{"v":"9"},{"v":"6"}]},{"c":[{"v":"164q"},{"v":5},{"v":"5"},{"v":"5"}]}]}
结合时
$result = array('objA' => $r_percent, 'objB' => $r );
echo json_encode($result);
更新的回声
{"objA":"{cols:[{\"id\":\"t\",\"label\":\"Title\",\"type\":\"string\"},{\"id\":\"n\",\"label\":\"Neutral(+) \",\"type\":\"string\"},{\"id\":\"a\",\"label\":\"Agree\",\"type\":\"string\"},{\"id\":\"ne\",\"label\":\"Neutral(-)\",\"type\":\"string\"},{\"id\":\"d\",\"label\":\"Disagree\",\"type\":\"string\"}],rows:[{\"c\":[{\"v\":\"165q\"},{\"v\":0},{\"v\":0},{\"v\":0},{\"v\":100}]},{\"c\":[{\"v\":\"160q\"},{\"v\":0},{\"v\":0},{\"v\":0},{\"v\":6}]},{\"c\":[{\"v\":\"161q\"},{\"v\":0},{\"v\":0},{\"v\":0},{\"v\":10}]},{\"c\":[{\"v\":\"162q\"},{\"v\":7},{\"v\":0},{\"v\":7},{\"v\":0}]},{\"c\":[{\"v\":\"163q\"},{\"v\":0},{\"v\":25},{\"v\":0},{\"v\":0}]},{\"c\":[{\"v\":\"164q\"},{\"v\":0},{\"v\":100},{\"v\":0},{\"v\":0}]}]}","objB":"{cols:[{\"id\":\"t\",\"label\":\"Title\",\"type\":\"string\"},{\"id\":\"l\",\"label\":\"Avg \",\"type\":\"string\"},{\"id\":\"lb\",\"label\":\"High\",\"type\":\"string\"},{\"id\":\"lo\",\"label\":\"Low\",\"type\":\"string\"}],rows:[{\"c\":[{\"v\":\"165q\"},{\"v\":1.3333333333333},{\"v\":\"2\"},{\"v\":\"1\"}]},{\"c\":[{\"v\":\"160q\"},{\"v\":6},{\"v\":\"10\"},{\"v\":\"1\"}]},{\"c\":[{\"v\":\"161q\"},{\"v\":6.6666666666667},{\"v\":\"9\"},{\"v\":\"2\"}]},{\"c\":[{\"v\":\"162q\"},{\"v\":7},{\"v\":\"9\"},{\"v\":\"3\"}]},{\"c\":[{\"v\":\"163q\"},{\"v\":8},{\"v\":\"9\"},{\"v\":\"6\"}]},{\"c\":[{\"v\":\"164q\"},{\"v\":5},{\"v\":\"5\"},{\"v\":\"5\"}]}]}"}