0

I am trying to echo inside Google charts but I am receiving this message Parse error: syntax error, unexpected '' . I tried to change all the possible combinations with quotes but still doesn't work. Can you please tell me where is the bug?

   <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Text', 'Number'],
          <?php 
          while ($row = mysqli_fetch_array($data_query)) {
            echo "['".$row['text']."', $row['number']. '"],'";
          }
        ]);

        var options = {
          title: 'My Daily Activities'
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));

        chart.draw(data, options);
      }
    </script>
  </head>
JohnMe
  • 3
  • 1

1 Answers1

0

Never echo data this way, Use json_encode and echo result of this function:

<?php
$data = [
    ['Text', 'Number'],
];
while ($row = mysqli_fetch_array($data_query)) {
    $data[] = [$row['text'], $row['number']];
}

Later, in your js just:

var data = google.visualization.arrayToDataTable(<?php echo json_encode($data)?>);
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • This works, but when I add another row $data[] = [$row['text2'], $row['number2']]; it isn't displayed correctly! – JohnMe Dec 10 '19 at 16:08
  • I don't know what is __correctly__. You have to find out yourself and ask another question if needed – u_mulder Dec 10 '19 at 16:24