douejuan9162 2014-02-04 18:41
浏览 90
已采纳

Foreach in a graph:Uncaught SyntaxError:Unexpected token <

I have a graph which works fine manually (data I put by myself).

$(function () {
// Use Morris.Area instead of Morris.Line
    Morris.Area({
        element: 'graph-area',
        padding: 10,
        behaveLikeLine: true,
        gridEnabled: false,
        gridLineColor: '#dddddd',
        axes: true,
        fillOpacity:.7,
        data: [      
            {period: '1', Visites: 10},
            {period: '2', Visites: 78},
            {period: '3', Visites: 97},
            {period: '4', Visites: 45},
        ],
        lineColors:['#E67A77'],
        xkey: 'period',
        ykeys: ['Visites'],
        labels: ['Visites'],
        pointSize: 0,
        lineWidth: 0,
        hideHover: 'auto'

    });
});

But then I want to display the data dynamically with a foreach. I can't get this to work :

$(function () {
// Use Morris.Area instead of Morris.Line
    Morris.Area({
        element: 'graph-area',
        padding: 10,
        behaveLikeLine: true,
        gridEnabled: false,
        gridLineColor: '#dddddd',
        axes: true,
        fillOpacity:.7,
        data: [<?php

foreach($ga->getResults() as $result)
{
  echo "{period: '".$result."',";
  echo " Visites: ".$result->getVisitors()."},</br>";
}

?>],
        lineColors:['#E67A77','#79D1CF','#79D1CF'],
        xkey: 'period',
        ykeys: ['Visites'],
        labels: ['Visites'],
        pointSize: 0,
        lineWidth: 0,
        hideHover: 'auto'

    });
});

I have this in console error : Uncaught SyntaxError: Unexpected token ­<

When I echo my foreach, I get the the following thing with the same symthax. It should work but it's not.

{period: '01', Visites: 1},
{period: '02', Visites: 1},
{period: '03', Visites: 3},
{period: '04', Visites: 4},
{period: '05', Visites: 1},
{period: '06', Visites: 5},
{period: '07', Visites: 4},
{period: '08', Visites: 2},
{period: '09', Visites: 6},
  • 写回答

1条回答 默认 最新

  • duangu9997 2014-02-04 18:47
    关注

    You're outputting html in your javascript. If you want to echo a newline use .

    foreach($ga->getResults() as $result)
    {
      echo "{period: '".$result."',";
      echo " Visites: ".$result->getVisitors()."},
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站