我有段JS代码里面嵌了块php代码,我现在要js循环三十次,同时将循环变量i每次都传递到php代码中,要怎么弄?求助啊
<script>
window.onload = function ()
{ for(i=0;i<30;i++)
var readpoint = <?php
if(id==i)
。
。
?>
.
.
.
.
}
<script>
window.onload = function ()
{ for(i=0;i<30;i++)
var readpoint = <?php
if(id==i)
。
。
?>
.
.
.
.
}
你这个30次,完全可以设置由一个人来做,php也有循环不是吗,什么?30是变量还有数据库等操作?想法告诉PHP这件事,让他把结果返回,不还是完成了任务吗,没必要两人一人一下过招
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>RGraph折线图</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="keywords" content="rgraph html5 canvas basic example" />
<meta name="description" content="RGraph: A basic example" /><!-- 1/3. Include the RGraph libraries -->
<script src="js/RGraph.common.core.js"></script>
<script src="js/RGraph.common.annotate.js"></script>
<script src="js/RGraph.common.context.js"></script>
<script src="js/RGraph.common.tooltips.js"></script>
<script src="js/RGraph.common.zoom.js" ></script>
<script src="js/RGraph.common.resizing.js"></script>
<script src="js/RGraph.common.dynamic.js" ></script>
<script src="js/RGraph.line.js" ></script>
<script type="text/javascript">
</script>
<!--[if IE]><script src="js/excanvas.compressed.js"></script><![endif]--><script>
window.onload = function ()
{
var readpoint = <?php
$con = mysql_connect("localhost", "root", "123456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
} $db_selected = mysql_select_db("physicaldata", $con); if (!$db_selected)
{
die ("Can\'t use physicaldata : " . mysql_error());
}
// $sql = "select * from ecg1 where id=3";
$sql='select * from ecg1 where id=';
$sql = $sql ."1";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
// echo $row['time'];
// echo "\n";
// echo $row['data'];
$array =array('time'=>$row['time'],'data'=>$row['data'],);
json_encode($array);
}
mysql_close($con);
echo json_encode($array[data]); ?>;
var arr = readpoint.split(",");
var line1 = new RGraph.Line('line1',arr);
line1.Set('chart.title', '血压');
line1.Set('chart.colors', ['red']);
// line1.Set('chart.ticks', 'circle');
line1.Set('chart.linewidth', 1);
line1.Set('chart.background.barcolor1', 'white');
line1.Set('chart.background.barcolor2', 'white');
line1.Set('chart.background.grid.autofit', true);
line1.Set('chart.filled', 'true');
line1.Set('chart.fillstyle', ['#fcc']);
//line1.Set('chart.crosshairs', true);
if (!document.all) {
// line1.Set('chart.tooltips', ["50","75","63","59","46","38","21","32","12","15","50","75","63","59","46","38","21","32","12","15"]);
line1.Set('chart.tooltips.effect', 'fade');
line1.Set('chart.contextmenu', [['Zoom in', RGraph.Zoom], ['Cancel', function () {}]]);
line1.Set('chart.zoom.delay', 10);
line1.Set('chart.zoom.frames', 25);
line1.Set('chart.zoom.vdir', 'center');
} line1.Set('chart.text.angle', 45);
line1.Set('chart.gutter', 45);
//line1.Set('chart.units.post', '%');
// line1.Set('chart.labels.ingraph', ["50","75","63","59","46","38","21","32","12","15","50","75","63","59","46","38","21","32","12","15"]);
line1.Set('chart.noaxes', false);
line1.Set('chart.background.grid', true);
line1.Set('chart.yaxispos', 'left');
line1.Set('chart.ymax', 450);
line1.Draw();
}</script>
</head><body><h1>血压监控数据</h1><canvas id="line1" width="1000" height="450">[请稍候 ...]</canvas>
</body>
// echo $row['time'];
// echo "\n";
// echo $row['data'];
array_push($array,array('time'=>$row['time'],'data'=>$row['data']));
}
json_encode($array);//这儿的数据是不是30组了?
不是,一组数据是个数组,$sql='select * from ecg1 where id=';
$sql = $sql ."1";这里才是第几组数据(这是id=1)
你把这句话进数据库查了以后,在这个 while里才出来多组数据,别搞错了,至少测试一下
不是的,我的目的是每循环一次,php从数据库读一组数据,返回给js的一个变量,并绘图,绘图代码是在Js里