我有段JS代码里面嵌了块php代码,我现在要js循环三十次,同时将循环变量i每次都传递到php代码中,要怎么弄?求助啊
<script>
window.onload = function ()
{    for(i=0;i<30;i++)
var readpoint = <?php
                           if(id==i)
                           。
                           。
                         ?>
 .
 .
 .
 .
}

解决方案 »

  1.   

    一个是前台语言一个是后台语言,虽然写在一起但不会同时工作的,你得到的数据时PHP早就完事了,JS还未开始,通信常采用json格式,ajax方式
    你这个30次,完全可以设置由一个人来做,php也有循环不是吗,什么?30是变量还有数据库等操作?想法告诉PHP这件事,让他把结果返回,不还是完成了任务吗,没必要两人一人一下过招
      

  2.   

    我的目的是每次循环让php返回readpoint(js变量)一个值,然后对readpoit处理一次,接着再循环。如果把循环设在php里面的话,完成不了这个目的吧?
      

  3.   


    <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>
      

  4.   

    php预处理的,服务器脚本语言,不可能像你说的那样执行的
      

  5.   

    while ($row = mysql_fetch_assoc($result)) {//你意思这儿有30组吗;其实输出显示就可在这里完成了
    // echo $row['time'];
    // echo "\n";
    // echo $row['data'];
    array_push($array,array('time'=>$row['time'],'data'=>$row['data']));
    }
    json_encode($array);
    //这儿的数据是不是30组了?
      

  6.   


    不是,一组数据是个数组,$sql='select * from ecg1 where id=';
    $sql = $sql ."1";这里才是第几组数据(这是id=1)
      

  7.   

    这是啥数据啊,这是个sql语句,是个字符串而以
    你把这句话进数据库查了以后,在这个 while里才出来多组数据,别搞错了,至少测试一下
      

  8.   

    感觉lz的逻辑有些问题,将循环变量i每次都传递到php代码中,不能在php中循环处理吗?需要和用户交互的话,不能存到数组然后传到php处理吗?
      

  9.   


    不是的,我的目的是每循环一次,php从数据库读一组数据,返回给js的一个变量,并绘图,绘图代码是在Js里
      

  10.   

    要门AJAX,要么打开30个新网页。除此别无他法。
      

  11.   

    你好,有几个RGraph的几个问题想请教一下?http://bbs.csdn.net/topics/390278455