javascript – 未捕获的SyntaxError:CodeIgniter中的意外标记<

我目前正在处理CodeIgniter图表,但收到的错误如下:

Uncaught SyntaxError: Unexpected token <

并且图表未加载显示空白.

var data_course_stats = google.visualization.arrayToDataTable([
          ['Course', 'Time spent',{ role: 'style' }],
           <?php 
           $i=0;
           foreach ($timespent_stats as $course) { $course = (object)$course;
           $color_val = 'green';
            if(count($i<count($timespent_stats)))
              $color_val = $colors[$i++];
            ?>
         ['<?php echo $course->title;?>', <?php echo $course->spent_seconds/60;?>,'<?php echo $color_val; ?>'],
         <?php } ?>
        ]);

        var options_course_stats = {
          title: 'Course Wise Spent Time in Minutes',
          curveType: 'function',
           height: 400,
             bar: {groupWidth: "50%"},
          legend: { position: "none" },
};

最佳答案 虽然你已经接受了答案,但我想添加另一种技术,它比前一种技术简单得多.你可以用<?= like<?php echo something执行echo; ?>所以你可以简单地执行此操作<?= something?>

<?php
    $i=0;
    foreach ($timespent_stats as $course) { 
    $course = (object)$course;
    $color_val = 'green';

    if(count($i<count($timespent_stats)))
        {
        $color_val = $colors[$i++];
?>
        [<?= $course->title ?>, <?= $course->spent_seconds/60 ?>, <?= $color_val ?>]
<?php
        }
    }
?>
点赞