doumeng3188 2013-06-13 07:43
浏览 65
已采纳

JavaScript PHP HighChart将PHP数组转换为javascript数组

I saw other posts but it doesn't work. I am a bit confused here on how I implement an array into JS from PHP...

In the PHP file (test.php) I have :

  $table = array();  
  while($row = mysqli_fetch_assoc($result) )  
  {

   $value=$row['value'];
   $date=$row['date'];

   $table[$value]=$date;

  }

And in JavaScript I have :

  <?php include 'test.php';  ?>

   ... 

  data: (function() {
  // generate an array of random data
   var data = [],
    time = (new Date()).getTime(),
    i;

   for (i = -19; i <= 0; i++) {
    data.push({
     x: time + i * 1000,
     y: Math.random()
    });
   }
   return data;

So what I look for is to put $value=$row['value']; in the y : and $date=$row['date']; in the x : OR perhaps putting the entire table $table in the var data will work also .

I'm new to JavaScript, so thanks in advance..!

  • 写回答

1条回答 默认 最新

  • dongyi1939 2013-06-13 07:49
    关注

    So in your php file....

    Add a line at the bottom that converts the table to json data. And give it a variable...

    $table = array();  
     while($row = mysqli_fetch_assoc($result) )  
     {
    
       $value=$row['value'];
       $date=$row['date'];
    
       $table[$value]=$date;
    
     }
    
     $jsondata = json_encode($table);
    

    Then in your other file.... echo that variable into your data object, in the javascript. Remember to remove that whole random number generating function...(its just an example)

    Echoing PHP into javascript is definitely not considered good practice though. And it would be better to actually do an ajax call to your php file, and insert like that....I'll also show you how to do ajax.

      <?php include 'test.php';  ?>
    
        ... 
    
       data: [<?php echo $jsondata;?>], //remove that function that was here..
       // it was just to generate random numbers for the demo
       ....
       }
    

    EDIT / UPDATE For ajax...

    So for ajax...instead of assigning a variable to $jsondata. Just return it like so...(in your PHP file)

     return json_encode($table);
    

    Then for this way....you dont include('test.php') like you did before. Instead you just have this script inside your $(document).ready(function(){....

    $.getJSON('test.php', function(myJSON) {
    
     //and inside this function you put your highcharts stuff...
     //remove that function() that generates random data
     // And you will put the 'myJSON' return object inside the 'data':[] array...
     // Provided you have structured your data correctly for highcharts, it should work...
     //  If not.... it'll be a start, and you're well on your way to debugging it
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?