-2

I want to get the data from the php variables into the pie chart as I tried to do it with $Utha and the value of $one in its position of the pie chart. How would this work with an example connecting from a database? How can I make multiple charts appear in the same page. For example more than one chart with different data each. Can these charts be centered.

<?php
$Utha = 'Utha';    
$Ilinois = 'Ilinoins';
$WD = 'Washington DC';
$Florida = 'Florida';
$California = 'California';

$one = 1;
$two = 2;
$three = 3;
$four = 4;
$five = 5;
?>
<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['$Utha',     $one],
          ['$Ilinois',  $two],
          ['$Florida',  $three],
          ['$California', $four],
          ['$WD',    $five]
        ]);

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

        var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="piechart_3d" style="width: 900px; height: 500px;"></div>
  </body>
</html>
2
  • 1
    You need to wrap and echo the php variables, <?php echo $one; ?> Commented Dec 9, 2015 at 10:48
  • A Better way is to Wrap all Data in PHP as <?php $data = [ [ 'Task', 'Hours per Day'], [ "Utha", 1 ], [ 'Ilinoins', 2 ], [ 'Washington DC', 3], [ 'Florida', 4 ], [ 'California', 5 ] ]; ?> AND then write var data = google.visualization.arrayToDataTable(<?php echo json_encode($data); ?>); in JavaScript Commented Dec 9, 2015 at 11:06

2 Answers 2

0

You can create variable in javascript based on PHP variable, example :

var one = '<?php echo $one; ?>';

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0
var demo = "<?php echo $phpVar; ?>";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.