1

I've created a Google chart and I've added data taken from the database itself. When getting data through php loops though, I have had some difficulty because I couldn't amend that data to the chart because of its syntax.

This is as far as I have gone. I need to get the mysql values in place of:

1170, 460, 45, 123, 456], 660, 1120, 145, 785, 658]

    var data = google.visualization.arrayToDataTable([
      ['Term', <?php

      while ($getchartrows =  mysql_fetch_array($getchart))
      {
          echo  " ' " . $getchartrows['std_ID'] . " ' " . ",";
      }

      ?>],

      <?php


      $one = mysql_query("SELECT * FROM stusitting WHERE std_ID = '0001' AND subjectNo = '$subin' AND grade = '$gradein' AND termNo = '$tcheck' ");
      $getone = mysql_fetch_array($one);

      ?>

          ['01',  <?php echo $getone ['marks']; ?>,      400, 495, 565, 415],
      ['02',  1170,      460, 45, 123, 456],
      ['03',  660,       1120, 145, 785, 658]
    ]);




    var options = {
      title: 'Class Progress'
    };

1 Answer 1

5

Try to tackle one problem after the other. First, get the data you need from the database. Then, create the data structure you need in PHP, and convert it to JavaScript using json_encode(). Finally, pass it to the visualization function in JavaScript:

<?php
// query data
$result = mysql_query(...);

// format data structure
$data = array();
$i = 0;
while($row = mysql_fetch_array($result)) {
    $i += 1;
    array_push($data, array($i) + $row);
}

// convert to JavaScript
?>
var raw_data = <?php echo json_encode($data) ?>;

// visualize
var data = google.visualization.arrayToDataTable(raw_data);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. i was able to do it using javascript. sorry for not updating the post.. :)
Say you want to select multiple columns in the query. In the while loop, how would you do this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.