dongtai419309
2014-10-17 07:08
浏览 52

在javascript中回显PHP数组

Answer from here : https://stackoverflow.com/a/12863675/894470 is how to assign array to variable.

I'm getting my data from php but when I echo it inside javscript like example below my chart wont draw.

var month_data= [<?php echo json_encode($lol); ?>];

but I can show alert and see all my data no problem like below:

enter image description here

If I copy the text from alert and paste it in array like below:

var month_data= [{month: '2014-01', KFC: 0, PizzaHut: 1},{month: '2014-03', KFC: 2, PizzaHut: 1},{month: '2014-04', KFC: 1, PizzaHut: 0},{month: '2014-05', KFC: 0, PizzaHut: 1},{month: '2014-07', KFC: 1, PizzaHut: 0},{month: '2014-10', KFC: 42, PizzaHut: 42}];

my chart will draw. What is wrong here??

UPDATE: My query:

$lol = array();
while ($row = mysqli_fetch_array($result)) {

    $lol[] =   "{month: '". $row['year'] ."-". $row['month'] ."', KFC: ". $row['kfc'] .", PizzaHut: ". $row['pizzahut'] ."}";
}

and I'm using Morris.js: http://morrisjs.github.io/morris.js/ to generate my chart.

图片转代码服务由CSDN问答提供 功能建议

从这里回答: https://stackoverflow.com/a/12863675/894470 是如何将数组赋值给变量。

我从php获取数据但是当我在里面回应它时 像我的图表下面的例子javscript不会画。

  var month_data = [&lt;?php echo json_encode($ lol);  ?&gt;]; 
   
 
 

但是我可以显示提醒并查看我的所有数据没有问题如下:

如果我从警报中复制文本并将其粘贴到 数组如下:

  var month_data = [{month:'2014-01',KFC:0,PizzaHut:1},{month:'2014-03',KFC  :2,PizzaHut:1},{month:'2014-04',KFC:1,PizzaHut:0},{month:'2014-05',KFC:0,PizzaHut:1},{month:'2014-  07',肯德基:1,PizzaHut:0},{月:'2014-10',肯德基:42,PizzaHut:42}]; 
   
 
 

我的图表 会画画。 这里有什么问题?

UPDATE :我的查询:

  $ lol = array();  
while($ row = mysqli_fetch_array($ result)){
 
 $ lol [] =“{month:'”。  $ row ['year']。“ - ”。  $ row ['month']。“',肯德基:”。  $ row ['kfc']。“,PizzaHut:”。  $ row ['pizzahut']。“}”; 
} 
   
 
 

我正在使用 Morris.js http://morrisjs.github.io/morris.js/ 生成我的图表。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drls2738 2014-10-17 07:37
    已采纳

    You need a real PHP structure to encode it into JSON, not just a string:

    $lol = array();
    while ($row = mysqli_fetch_array($result)) {
        $lol[] = Array(
            "month"    => $row['year'].'-'.$row['month'],
            "KFC"      => $row['kfc'],
            "PizzaHut" => $row['pizzahut']
        );
    }
    

    and:

    var month_data = <?php echo json_encode($lol); ?>;
    

    json_encode will automatically translate $lol into a json array containing objects (associative arrays are translated into objects).

    打赏 评论
  • douyi8315 2014-10-17 07:12

    All is fine, you just have to parse it to json format, you can do that using:

    var month_data= JSON.parse('<?php echo json_encode($lol); ?>');
    

    edit your php code like this:

    $lol[] =   array("month" => $row['year'], "KFC" => $row['kfc'] ...
    

    anyway i do not recommend using php in js, the better way would be to put the content from the variable somewhere in html and than to take it with js.

    打赏 评论

相关推荐 更多相似问题