Chartjs-How to insert php in data chart.js

1👍

Adding to the previous comments, PHP Array are not the same thing as JavaScript array. There are lots of ways this can be done, one is to loop through the $timers array in PHP and then convert that to corresponding JavaScript

Assuming the time contains array('label'=>'Today','data'=>55);

then you can use foreach or for to build the corresponding JavaScript array

<script type='text/javascript'>
    let data=[];// this is a javascript array

    <?php 
    $timersCount=count($timers);
    for($i=0; $i<$timersCount i++){
    ?>
       data.push({
           data:<?php echo $timers[$i]['data']?>,
           label:<?php echo $timers[$i]['label']?>
        });
    <?php
    }?>
</script>

There might be typo, i just typed it but you get the idea.
I am assuming you are running the script in a PHP view file.

Leave a comment