sairam gouni
sairam gouni

Reputation: 85

Uncaught SyntaxError: Unexpected token < in CodeIgniter

I am currently working on CodeIgniter charts, but am getting an error like:

Uncaught SyntaxError: Unexpected token <

And charts are not loading showing blank.

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" },
};

Upvotes: 4

Views: 5136

Answers (3)

Eddie Tower
Eddie Tower

Reputation: 11

If anyone get this problem again, check if your base_url is correct in config/config.php

Upvotes: 0

always-a-learner
always-a-learner

Reputation: 3794

although you have accepted the answer, i want to add the another technique which is little simpler than the previous one. You can perform echo with <?= like <?php echo something; ?> so you can simply do this <?= 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
        }
    }
?>

Upvotes: 0

Scott C Wilson
Scott C Wilson

Reputation: 20016

For longer blocks, to keep PHP open - you're getting in trouble because you're mixing and matching open and closed. Change this:

       <?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 } ?>

to this:

<?php 
       $i=0;
       foreach ($timespent_stats as $course) { 
         $course = (object)$course;
         $color_val = 'green';
         if(count($i<count($timespent_stats))) {
            $color_val = $colors[$i++];

            echo "['" . $course->title . "','" .  
                        $course->spent_seconds/60 . "','" . 
                        $color_val . "']"; 
         }
       }
       ?>

Upvotes: 1

Related Questions