我目前正在尝试开始使用php进行一些程序代,并希望用条形图显示我的函数:
<?php
$seed = 58521672597513854781922;
global $randomArray;
for ($i=-1000; $i<1001; $i++) {
$temp = $seed/($i+0.1)^0.5;
$tempRes = $temp.'';
$randomArray[$i] = $tempRes[5];
}
?>
<div>
<canvas id="c3" width="600" height = "200" style="border:solid 1px #000000;"></canvas>
<div>
<button onclick="Vertical_line();return true;">draw array</button>
</div>
</div>
<script>
var c3 = document.getElementById("c3");
var c3_context = c3.getContext("2d");
function Vertical_line() {
<?php
for($j=0;$j<sizeof($randomArray);$j++){
$height = 100 - 10*$randomArray[$j];
?>
c3_context.moveTo(<?php echo 300+$j;?>, 200);
c3_context.lineTo(<?php echo 300+$j;?>, <?php echo $height;?>);
c3_context.strokeStyle = "red";
c3_context.stroke();
<?php }?>
}
</script>
我不明白为什么变量$ j作为我的数组中的键引用不起作用。如果我手动用数字替换它来绘制我的数组的单个条目它没有问题,如果我将变量设置为绝对数字并使用它作为键也可以工作,只是当我使用一个更改其值的变量时它不会显示任何东西。
感谢帮助:)
好的,感谢帮忙,发现错误,我的数组的负键是一个问题,简单的疏忽:),感谢@ u_mulder提醒我查看源代码,我不得不滚动很长一段时间才能看到错误^^
你不能在JavaScript内部运行Php,Php总是在页面加载后运行,你想创建一个JS函数来完成你希望的工作