dsbezji539113152 2019-05-16 06:29
浏览 100

在javascript循环中增加php变量

Im trying to create a display of charts using chartJS that are dynamic in both the number of charts displayed and its contents. I am stuck as to how to iterate through the result set in php while looping through the creation of charts in javascript. I know that Javascript is processed client side and php is processed server side. I wrote this code to give you guys a general idea of what i am trying to do. Right now the counter does not increase and i end up with the same graphs, whatever data that comes first in the array. In a sense, i need some way of tracking the var i in javascript within php to access the contents in my result set.

var charts = [];
    <?php $counter = 0; ?>

    for (var i = 0; i < <?php echo sizeOf($readingArray); ?>; i++) {
        var ctx = document.getElementById(i);
        charts[i] = new Chart(ctx, {
        type: 'line',
        data: {
            datasets: [{
            label: 'Actual Readings',
            data: <?php echo json_encode($readingArray[$counter][4], JSON_NUMERIC_CHECK); ?>,
        }, {
            label: 'Threshold',
            data: <?php echo json_encode($readingArray[$counter][5], JSON_NUMERIC_CHECK); ?>,
        }],
        labels: <?php echo json_encode($readingArray[$counter][3]);?>,

        },
        options: {
                zoom: {
                        enabled: true,
                        mode: 'x',
                },
                pan: {
                       enabled: true,
                       mode: 'x',
                },

        },
    });
    <?php $counter++; ?>
  }
  • 写回答

1条回答 默认 最新

  • dongzongzhi6953 2019-05-16 06:42
    关注

    You shout assign php variables to js variables and work with them. Something like this

    var data = '<?php echo json_encode($readingArray); ?>';
    

    Then convert json string to object

    arrayOfObjects = JSON.parse(data)
    

    After that iterate your data and print chart

    for (var key in arrayOfObjects) {
    console.log(arrayOfObjects[key])
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)