1

I am trying to draw two graphs using chartjs. I want to make my life simplier by using a for loop to declare the variables required for the chart object.

The thing is I have created a 2d array, with each row storing data for current year, the next row storing data for the consecutive year and so on. I am trying to access the row of the variable using loop.

Here is my Chart obj


   var canvas6= {
      type: 'doughnut',
      data: {
        datasets: [{
          data: [
          <?php 
             for($i=0;$i<count($dataAgeGrp[1]);$i++){  -------->Note here
                echo $dataAgeGrp[1][$i];    -------->And here
                echo ',';
             }
          ?>
          ],
          backgroundColor: [
          <?php
          for($i=0;$i<count($ageCategory);$i++){
             $rand = str_pad(dechex(rand(0, 0xFFFF00)), 6, 0, STR_PAD_LEFT);
             echo('"#' . $rand.'"');
             echo ",";
          }

          ?>
        
         ],
          label: 'Pie Chart'
        }],
        labels: [
        <?php 
             for($i=0;$i<count($ageCategory);$i++){
                echo $ageCategory[$i];
                echo ',';
             }
          ?>
        ],

      },
      options: {
        responsive: true
      }
    };
$(function () { 
      var ctx126 = document.getElementById('canvas6').getContext('2d');
      window.myPie = new Chart(ctx126  , canvas6); 
  });

So, I tried something like this

for(var k=0;k<3;k++){
  var q=26;
 var canvas+q = {
      type: 'doughnut',
      data: {
        datasets: [{
          data: [
          <?php 
             for($i=0;$i<count($dataAgeGrp[k]);$i++){
                echo $dataAgeGrp[k][$i];
                echo ',';
             }
          ?>
          ],
          backgroundColor: [
          <?php
          for($i=0;$i<count($ageCategory);$i++){
             $rand = str_pad(dechex(rand(0, 0xFFFF00)), 6, 0, STR_PAD_LEFT);
             echo('"#' . $rand.'"');
             echo ",";
          }

          ?>
        
         ],
          label: 'Pie Chart'
        }],
        labels: [
        <?php 
             for($i=0;$i<count($ageCategory);$i++){
                echo $ageCategory[$i];
                echo ',';
             }
          ?>
        ],

      },
      options: {
        responsive: true
      }
    };

  $(function () { 
      var ctx1+q = document.getElementById('canvas'+q).getContext('2d');
      window.myPie = new Chart(ctx1+q , canvas+q); 
  });
  q=q+1;

But I am getting this error

Use of undefined constant k - assumed 'k' (this will throw an Error in a future version of PHP)

How do I fix it?

Skumar
  • 480
  • 6
  • 24
  • save yourself a lot of rendering time, replace `for($i=0;$i – Tschallacka May 11 '21 at 07:31
  • Does this answer your question? [How can you use php in a javascript function](https://stackoverflow.com/questions/8471945/how-can-you-use-php-in-a-javascript-function) – Tschallacka May 11 '21 at 07:34
  • 4
    This is generally a bad way of doing things (for this purpose). Prepare any data you have in PHP, encode it as JSON and store it in a JS variable so you can do all the looping inside JS without having to rely on PHP any longer. – El_Vanja May 11 '21 at 07:36
  • @Tschallacka No – Skumar May 11 '21 at 08:57
  • @El_Vanja I already understood that. Because the performance of the page is quite low. But, I dont know/dont have any documents to refer to , to do what you said. Please give me some hint – Skumar May 11 '21 at 08:58
  • Here's a pretty good starting point: [How do I pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript) – El_Vanja May 11 '21 at 09:00

1 Answers1

0

If you can use PHP variables in your JavaScript code, the contrary is not possible. The only way to pass JavaScript variable to PHP code is by request (reload page with GET/POST parameters or AJAX request)

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
DarkCid
  • 227
  • 1
  • 3
  • 18