0

I used Google graph to draw a graph by below code, but the problem is that my data is on a sql table, how can I put my table variables into this graph?

<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([
      ['Year', 'Sales', 'Expenses'],
      ['2004',  1000,      400],
      ['2005',  1170,      460],
      ['2006',  660,       1120],
      ['2007',  1030,      540]
    ]);

    var options = {
      title: 'Company Performance'
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
7
  • You must SELECT the data on the php file where you prepare this page you posted and then echo the data as the format you show in your sample code. Commented Jul 29, 2013 at 11:13
  • I want your sql table structure Commented Jul 29, 2013 at 11:13
  • You first need to extract the data from the database - the recommend approach for PHP in this day and age is to just PHP Data Objects Commented Jul 29, 2013 at 11:15
  • @SnowBlind number of rows are not specified, every time it changed, how to echo them? Commented Jul 29, 2013 at 11:27
  • @r.vengadesh I have table named result with two column: attempt and login, every time that visitor loged in one row adds Commented Jul 29, 2013 at 11:29

3 Answers 3

0
<?php


$result = mysql_query("SELECT year,sales,expenses from tablename");
$value=array();
while($r = mysql_fetch_assoc($result)) {
$year=$r['year'];
$sales=$r['sales'];
$expenses=$r['expenses'];
$val="[".$year.",".$sales.",".$expenses."]";
array_push($value,$val );
}
$final_value = implode(",", $value);

?>

put this $final_value into google chart

function drawChart() {
    var data = google.visualization.arrayToDataTable([
      <?php echo $final_value?>
    ]);

    var options = {
      title: 'Company Performance'
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
Sign up to request clarification or add additional context in comments.

2 Comments

what is $array in $final_value = implode(",", $array); ?
sorry change $array into $ value @SunnyLeone
0

Retrieve your data From tables by querying database and convert it to JSON string. Then pass your json string to your variable

var data=/*your json string*/

2 Comments

I am new to PHP and SQL, could you help me more, how to do this?
You can get some ideas by visiting posts like this stackoverflow.com/questions/10045341/…
0
$db = new mysqli('localhost', 'user', 'pass', 'demo');

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

$sql = "SELECT attempt, login FROM result";

if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}

echo "var data = google.visualization.arrayToDataTable([['Attempt', 'Login']";

while($row = $result->fetch_assoc()){
    echo ",['".$row['attempt'] ."','".$row['attempt']."']";
}

echo "]);";

$result->free();

$db->close();

2 Comments

there is error on echo "var data = google.visualization.arrayToDataTable([['Attempt', 'Login']";
@SunnyLeone Editted the answer, please try again.

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.