Using PHP in Google Line Chart API

306 views
Skip to first unread message

Animesh Pandey

unread,
May 4, 2012, 3:28:01 PM5/4/12
to Google Chart API
<?php
$mid = $_GET['id'];
//echo $mid;
// Connect to database server
$hd = mysql_connect("localhost", "root", "")
or die ("Unable to connect");

// Select database
mysql_select_db ("movie", $hd)
or die ("Unable to select database");

// Execute sample query
$res = mysql_query("SELECT * FROM ratings WHERE movieid = $mid",
$hd)
or die ("Unable to run query");

$res1 = mysql_query("SELECT Title FROM movies WHERE movieid = $mid",
$hd)
or die ("Unable to run query");

// Query number of rows in rowset
$nrows = mysql_num_rows($res);

// Output
echo "The query returned $nrows row(s):\n\n <br>";
$rates = array();
$users = array();

// Iteration loop, for each row in rowset
while ($row = mysql_fetch_assoc($res))
{
// Assigning variables from cell values
$data1 = $row["UserID"];
$data2 = $row["MovieID"];
$data3 = $row["Rating"];
array_push($rates, $data3);
array_push($users, $data1);

echo "$data1 $data3<br>";
}
$row1 = mysql_fetch_assoc($res1);

function stddev($array){
$n = 0;
$mean = 0;
$M2 = 0;
foreach($array as $x){
$n++;
$delta = $x - $mean;
$mean = $mean + $delta/$n;
$M2 = $M2 + $delta*($x - $mean);
}
$variance = $M2/($n - 1);
return sqrt($variance);
}

$sum = array_sum($rates);
$average_rating = $sum/count($rates);
echo $row1["Title"];
echo "<br>";
echo "The average rating is $average_rating!";
echo "<br>";
echo "The Standard Deviation of the ratings is " . stddev($rates);
echo "<br>";
mysql_close($hd);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/
>
<title>
</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></
script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['corechart']});
</script>

<?php
echo "<script>\n";
echo "var userArray = new Array();\n";
echo "var ratingsArray = new Array();\n";
foreach ($users as $i) {
echo "userArray.push($i)\n";
}
foreach ($users as $i) {
echo "ratingsArray.push($i)\n";
}
echo "</script>";
?>
<script type="text/javascript">
for (var i = 0; i < 10; ++i) {
document.write(userArray[i]);
document.write("\n");
}
</script>
<script type="text/javascript">
function drawVisualization() {
// Create and populate the data table.
var data = google.visualization.DataTable();

data.addColumn('number','UserID');
data.addColumn('number','Rating');

for (var i = 0; i < <?php echo $total;?>; ++i) {
data.addRow([ [userArray[i], ratingsArray[i]] ]);
}
// Create and draw the visualization.
new
google.visualization.LineChart(document.getElementById('visualization')).
draw(data, {curveType: "function",
width: 1000, height: 800,
vAxis: {maxValue: 6}}
);
}
google.setOnLoadCallback(drawVisualization);
</script>
</head>
<center>
<body style="font-family: Arial;border: 0 none;">
<div id="visualization" style="width: 500px; height: 400px;"></
div>
</body>
</center>
</html>

I want to plot the chart with the values I retrieved from the
Database. But I am not able to!
Everything else is working fine in the code!

The functions used for the API were taken from the documentation of
Line Chart API -
https://developers.google.com/chart/interactive/docs/reference

Please help!
Reply all
Reply to author
Forward
0 new messages