我想在单击li时更新饼图的数据。如果data.php$cid得到固定值,它就工作了。示例:user_student.cid=1将起作用。如果我使用user_student.cid=$cid,它就不起作用。有人知道吗?
谢谢你,pquest。
但也存在一些问题。如果我使用固定值1,则警报值为:[["apple",1],["banana",2],["orange",3]],馅饼正在工作。如果我使用$cid值(如果$cid的值为1),则值警报为:[["apple",1],["banana",2],["orange",3]],但馅饼不起作用。
有人知道吗?
<script type="text/javascript">
$(document).ready(function() {
$('table.display').dataTable({
"paging": false,
"info": false
});
$('li').click(function(){
var cid = $(this).attr("id");
$.ajax({
type: "POST",
url: 'data.php',
data:{ cid:cid },
success:function(reponse){
alert(reponse)
/*options.series[0].data = reponse;
chart = new Highcharts.Chart(options);*/
}
});
var options = {
chart: {
renderTo: 'pie',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'Web Sales & Marketing Efforts'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage}%</b>',
percentageDecimals: 1,
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ Highcharts.numberFormat(this.percentage, 1); +' %';
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ Highcharts.numberFormat(this.percentage, 1); +' %';
}
}
}
},
series: [{
type: 'pie',
dataType: 'json',
name: 'Browser share',
data: []
}]
}
$.getJSON("data.php", function(json) {
options.series[0].data = json;
chart = new Highcharts.Chart(options);
});
});
});
</script>
data.php
<?php
require_once("../connectDB.php");
$cid = isset($_POST["cid"]) ? $_POST["cid"] : "";
$result = $db->prepare("SELECT stu_info.reference FROM stu_info , user_student WHERE stu_info.sid = user_student.sid and user_student.cid = :cid");
$result ->execute(array(':cid' => $cid,));
$one = 0; $two = 0; $three = 0;
while($r = $result -> fetch()) {
if($r['reference'] === "apple")
$one = $one+1;
else
if($r['reference'] === "banana")
$two = $two+1;
else
if($r['reference'] === "orange")
$three = $three+1;
}
$ra = array($one,$three,$two);
$rb = array(apple,banana,orange);
$rows = array();
for($i=0 ; $i<=2 ; $i=$i+1)
{
$row[0] = $rb[$i];
$row[1] = $ra[$i];
array_push($rows,$row);
}
print json_encode($rows, JSON_NUMERIC_CHECK);
?>
不能只在查询文本中写入$cid
。你必须建立字符串:
"SELECT stu_info.reference FROM stu_info , user_student WHERE stu_info.sid = user_student.sid and user_student.cid = " . $cid
编辑:php中的串联运算符似乎是.
而不是+