简体   繁体   English

Javascript中的PHP变量

[英]Php variables inside Javascript

I am creating a doughnut chart and where I am having trouble is all of my data is being pulled server side. 我正在创建一个甜甜圈图,而我遇到麻烦的是我的所有数据都被拉到了服务器端。 Here is the html for the chart with set values. 这是带有设置值的图表的html。 How would I go about echoing my php variables as the values? 我将如何回显我的php变量作为值?

<html>
<head>
<script src="Chart.js"></script>
<style>
body{
padding: 0;
margin: 0;
}
#canvas-holder{
width:25%;
}
</style>
</head>
<body>
<div id="canvas-holder">
<canvas id="chart-area" width="500" height="500"></canvas>
</div>
<script>
var doughnutData = [
{
value: 500,
color:"#941616",
highlight: "#FF0000",
label: "Needs Agreement"
},

{
value: 500,
color: "#575757",
highlight: "#777777",
label: "Pre-Production"
},

{
value: 500,
color: "#aaaaaa",
highlight: "#cccccc",
label: "In Production"
}
];
window.onload = function(){
var ctx = document.getElementById("chart-area").getContext("2d");
window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, {responsive : true});
};
</script>
</body>
</html>

My php code grabs the data that is need via api and then stores each value in a variable eg. 我的PHP代码通过api获取所需的数据,然后将每个值存储在变量中。

<?php
$needs_agreement = 148000;
$pre_produciton = 137000;
$in_production = 3678000;
?>

Again, what is the best method for echoing the php variables where value: is above? 同样,在value:上面的位置上回显php变量的最佳方法是什么?

UPDATE: 更新:

var doughnutData = [
{
value: <?php echo $needs_agreement ?>,
color:"#941616",
highlight: "#FF0000",
label: "Needs Agreement"
},

{
value: <?php echo $pre_production ?>,
color: "#575757",
highlight: "#777777",
label: "Pre-Production"
},

{
value: <?php echo $in_production ?>,
color: "#aaaaaa",
highlight: "#cccccc",
label: "In Production"
}
];

The Above code which was suggested as the answer did not work. 被建议作为答案的上述代码不起作用。

Nothing stops you from simply echo'ing PHP variables in the middle of the javascript. 没有什么能阻止您简单地在javascript中间回显PHP变量。

<script>
var doughnutData = [
{
value: <?php echo $some_data ?>,
color:"#941616",
highlight: "#FF0000",
label: "Needs Agreement"
}
</script>

All of the PHP is processed before sending the document to the browser, so the javascript will be complete by the time it gets executed by the client. 所有PHP都会在将文档发送到浏览器之前进行处理,因此javascript将在客户端执行时完成。

I finally got it working, at the very end up of my php file: 我终于使它工作了,在我的php文件的最后:

echo '<script>' . 'var zx =' . $total_need . '</script>';
echo '<script>' . 'var zy =' . $total_pre . '</script>';
echo '<script>' . 'var zz =' . $total_in . '</script>';

The html section would then look like this: html部分将如下所示:

 var doughnutData = [ { value: zx, color:"#941616", highlight: "#FF0000", label: "Needs Agreement" }, { value: zy, color: "#575757", highlight: "#777777", label: "Pre-Production" }, { value: zz, color: "#aaaaaa", highlight: "#cccccc", label: "In Production" } ]; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM