douejuan9162
2014-02-04 18:41 阅读 69
已采纳

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 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()."},
    ";
    }
    
    点赞 评论 复制链接分享

相关推荐