Uncaught SyntaxError: Unexpected Token <in CodeIgniter

I am currently working on CodeIgniter diagrams, but getting an error, for example:

Untrained SyntaxError: Unexpected Token <

And the charts do not load showing a space.

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" },
};
+4
source share
3 answers

although you accepted the answer, I want to add another technique that is slightly simpler than the previous one. You can echo with <?=how <?php echo something; ?>so you can just do it<?= 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
        }
    }
?>
0
source

For longer blocks to keep PHP open - you have problems because you mix and match 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 } ?>

:

<?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 . "']"; 
         }
       }
       ?>
+1

- , base_url config/config.php

0

Source: https://habr.com/ru/post/1682706/


All Articles