图中的Foreach:Uncaught SyntaxError:意外的令牌<

时间:2014-02-04 18:41:36

标签: php graph foreach

我有一个手动工作正常的图表(我自己提供的数据)。

$(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'

    });
});

但后来我想用foreach动态显示数据。 我不能让这个工作:

$(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'

    });
});

我在控制台错误中有这个: 未捕获的SyntaxError:意外的标记&lt;

当我回应我的foreach时,我会用相同的symthax获得以下内容。它应该有效,但事实并非如此。

{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 个答案:

答案 0 :(得分:1)

您在javascript中输出html。如果您想使用\n来回显换行符。

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