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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?