我意识到你不能在脚本中使用php,但我很难用SQL数据填充图表。这是代码的简化版本。我已经在这里阅读了其他确切的问题,但根本无法让它发挥作用..希望有人可以提供帮助,谢谢
<?php
$query = mysql_query("SELECT count(green), count(amber), count(red) FROM cards");
$myData = mysql_fetch_assoc($query);
$green = $myData['count(green)'];
$amber = $myData['count(amber)'];
$red = $myData['count(red)'];
?>
<div id="students" style="height: 250px;">
<script type="text/javascript">
Morris.Donut({
element: 'students',
data: [
{value: '<?php $green; ?>', label: 'Green'},
{value: '<?php $amber; ?>', label: 'Amber'},
{value: '<?php $red; ?>', label: 'Red'},
],
formatter: function (x) { return x }
}).on('click', function(i, row){
console.log(i, row);
});
</script>
</div>
答案 0 :(得分:0)
$query = mysql_query("SELECT count(green) as `cgreen`, count(amber) as `camber`, count(red) as `cred` FROM cards");
$query = mysql_query("SELECT count(`green`) as `cgreen`, count(`amber`) as `camber`, count(`red`) as `cred` FROM `cards`");
$json = json_encode($myData);
var tmpd = JSON.parse('<?=$json; ?>');
Morris.Donut({
element: 'students',
data: [
{value: tmpd.cgreen, label: 'Green'},
{value: tmpd.camber, label: 'Amber'},
{value: tmpd.cred, label: 'Red'},
],
formatter: function (x) { return x }
}).on('click', function(i, row){
console.log(i, row);
});