-
-
Save jahidblackrose/5bd369d56d0959030349e15e1c8ef865 to your computer and use it in GitHub Desktop.
Populate Google Charts via PHP database query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Weight Tracker</title> | |
</head> | |
<body> | |
<?php | |
$username = ""; | |
$password = ""; | |
$hostname = ""; | |
//connect to database | |
$dbhandle = mysql_connect($hostname, $username, $password) | |
or die("Unable to connect to MySQL"); | |
echo "Connected to MySQL<br>"; | |
//select database | |
$selected = mysql_select_db("db541682804",$dbhandle) | |
or die("Could not select examples"); | |
//execute query | |
$result = mysql_query("SELECT id, date, weight FROM weight_tracker"); | |
//fetch data | |
while ($row = mysql_fetch_array($result)) { | |
$entry .= "['".$row{'date'}."',".$row{'weight'}."],"; | |
} | |
//close the connection | |
mysql_close($dbhandle); | |
?> | |
<div id="chart_div" style="width: 100%; height: 500px;"></div> | |
<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([ | |
['Date', 'Weight'], | |
<?php echo $entry ?> | |
]); | |
var options = { | |
title: 'Weight Tracker', | |
curveType: 'function', | |
legend: { position: 'bottom' } | |
}; | |
var chart = new google.visualization.LineChart(document.getElementById('chart_div')); | |
chart.draw(data, options); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment