gznero gznero - 6 months ago 64
MySQL Question

(PHP, Google Chart) Pie Chart using data in the PHP page

I'm new to PHP, I tried to create the pie chart on my PHP page but it shows nothing.

These are my codes (I put all codes in HTML section)

-> https://jsfiddle.net/1a4819vz/1/

and this the piechart.php parts

<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {

var data = google.visualization.arrayToDataTable([
['Position', 'Applicant(s)'],

<?php
for($i = 0; $i < $table_counter; $i++){
?>

[<?=$pos_chart[$i]?>, <?=$tapp_chart[$i]?>],

<?php
}
?>

]);

var options = {
title: 'Pie chart of Total Applicants'
};

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

chart.draw(data, options);
}
</script>

<div id="piechart" style="width: 900px; height: 500px;"></div>


The result for pie chart turns out only the big whole white space like this.

enter image description here

I don't know why. Please help.

Answer

the array values aren't being printed to JavaScript

need to use something like

<?=$pos_chart[$i]?>

or

<?php echo $tapp_chart[$i]?>

and don't see the need for the if statement

try this...

var data = google.visualization.arrayToDataTable([
  ['Position', 'Applicant(s)'],

  <?php
    for($i = 0; $i < $table_counter; $i++){
  ?>

  [<?="'".$pos_chart[$i]."'"?>,   <?=$tapp_chart[$i]?>],

  <?php
    }
  ?>

]);