[英]How can I access a PHP variable in Javascript?
I'm making a pie chart of expenses using JavaScript and CodeIgniter.我正在使用 JavaScript 和 CodeIgniter 制作费用饼图。
Controller控制器
chart_c
function getchart1() {
$result['query']=$this->model_m->getchart2();
$this->load->view('chartview',$result);
}
model_m
function getchart2() {
$query = $this->db->get('qpaper');
return $query->result();
}
View看法
foreach($query as $row) {
$ex=$row->expenses;
$cat=$row->category;
}
How can I access $row->expenses
and $row->category
from the script above in the following JavaScript?如何在以下 JavaScript 中从上面的脚本访问
$row->expenses
和$row->category
?
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],////how i will insert the data from db
['Work', 11],///my doubt here $e how i will put here
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
The full code:完整代码:
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],////how i will insert the data from db
['Work', 11],///my doubt here $e how i will put here
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
title: 'My Daily Activities'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="piechart" style="width: 900px; height: 500px;"></div>
</body>
</html>
If its just you want a pie chart using php array, you could use Highcharts .如果它只是你想要一个使用 php 数组的饼图,你可以使用Highcharts 。 Check their Edit in Jsfiddle page.
检查他们在 Jsfiddle 页面中的编辑。 You just need to pass data in json format to generate the chart.
您只需要以json格式传递数据即可生成图表。 I think you already have php array, convert it into json format and pass it to this highchart function.
我想你已经有了 php 数组,将其转换为 json 格式并将其传递给这个 highchart 函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.