我一直在尝试填充要在 jquery jqplot 插件/扩展中使用的数组。
据我所知,我的两个不同的 JSON 对象/数组在结构上是相同的,但只有“静态”对象/数组按预期工作。我在 stackoverflow 上看到过类似的问题,但没有一个对我有用。
我检查了 get_chart_votes 的源中是否返回了任何空格,但我找不到任何东西。使用最新的 CodeIgniter 框架、jQuery 和 jqplot。
使用静态 $arr 时,我可以提醒结果以获取 [Object object] 但是当尝试对 $charArray 执行相同操作时,我什么也得不到。
返回静态 $arr 时,它会打印此 json
{"test":1,"test2":2,"test3":3}
使用 mysql 结果返回 $chartArray,它会打印这个 json
{"Yes":1,"No":2,"Maybe":6}
我的 PHP 代码(注释掉了 $chartArray 回显)
function get_chart_votes()
{
$arr = array('test' => 1, 'test2' => 2, 'test3' => 3);
die(json_encode($arr));
$chartArray = array();
$post = $this->input->post();
$id = $post['id'];
$answers = $this->get_answers($id)->result_array();
$votes = $this->get_votes($id)->result_array();
$count = 0;
$answer = 1;
foreach($answers[0] as $ans)
{
if($count > 1 && $ans != "")
{
$vote = $votes[$answer-1]['COUNT(vote)'];
$chartArray[$ans] = intval($vote);
$answer++;
}
$count++;
}
//echo(json_encode($chartArray));
}
在客户端,我有这个。
var s1 = [];
var ticks = [];
$.getJSON(
"../votr/get_chart_votes",
{ id: "57"},
function(jsonData)
{
for (var key in jsonData)
{
if (jsonData.hasOwnProperty(key))
{
s1.push(jsonData[key]);
ticks.push(key);
}
}
$.jqplot.config.enablePlugins = true;
plot1 = $.jqplot('chart1', [s1], {
// Only animate if we're not using excanvas (not in IE 7 or IE 8)..
animate: !$.jqplot.use_excanvas,
seriesDefaults:{
renderer:$.jqplot.BarRenderer,
pointLabels: { show: true }
},
axes: {
xaxis: {
renderer: $.jqplot.CategoryAxisRenderer,
ticks: ticks
}
},
highlighter: { show: true }
});
$('#chart1').bind('jqplotDataHighlight',
function (ev, seriesIndex, pointIndex, data) {
$('#info1').html(ticks[pointIndex] + " got " + s1[pointIndex]);
}
);
}
);