Good Morning,
I am working on google charts to create a visualization chart using information in the database. But I have tried the possible ways i can think to solve this issue as well as I am new for PHP and Mysql. Can someone help me with the code.
<?php
// include database parameters
// set the db connection
$host ="";
$user="";
$password="(_A";
hash('',$);
$port="";
$schema="";
// done with connection
$s1date=$_GET['s1date'];
$s2date=$_GET['s2date'];
$var=$_GET['var'];
$hardcoded_product_id=$_GET["id"];
$conn = new mysqli($host, $user, $password, $schema , $port);
if ($conn->connect_error) {
die('Could not connect: ' .$conn->connect_error);
echo "Please contact you System administrator";
}
// set the variables if they are not set to initial
if(!$s1date && !$s2date && !$var){
$s1date='2017-03-24';
$s2date='2016-03-24';
$var='totalrain_mm';
}
// create season 1 query
$sql_s1date="select date," .$var . " from c_weather
where date between '" .$s1date ."' and DATE_ADD('" .$s1date ."',INTERVAL 60 DAY) ; ";
$res_sql_s1date= $conn->query($sql_s1date);
$counter=0;
while($row=$res_sql_s1date->fetch_assoc()) {
$s1_results[$counter]=array($row['date'],$row[$var]);
$s1_res_str.=$row[$var] .',';
$counter++;
}
// create season 2 query
$sql_s2date="select date," .$var . " from c_weather
where date between '" .$s2date ."' and DATE_ADD('" .$s2date ."',INTERVAL 60 DAY) ; ";
$res_sql_s2date= $conn->query($sql_s2date);
$counter=0;
while($row=$res_sql_s2date->fetch_assoc()) {
$s2_results[$counter]=array($row['date'],$row[$var]);
$s2_res_str.=$row[$var] .',';
$counter++;
}
// get the last date on database - 60 days
$sql_lastdate='SELECT DATE_SUB(date,INTERVAL 60 DAY) AS lastdate FROM c_weather order by lastdate DESC limit 1;';
$res_sql_lastdate= $conn->query($sql_lastdate);
while($row=$res_sql_lastdate->fetch_assoc()) {
$lastdate=$row['lastdate'];
}
?>
<html>
<head>
<script type="text/javascript" src="
https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['line']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Day');
data.addColumn('string', 'Season 1');
data.addColumn('string', 'Season 2');
<?php
foreach($result as $res )
{
echo "data.addRow(['{$res['$s1_res_str']}', {$res['$s1_res_str2']}]);";
}
?>
var options = {
chart: {
title: 'Box Office Earnings in First Two Weeks of Opening',
subtitle: 'in millions of dollars (USD)'
},
width: 900,
height: 500,
axes: {
x: {
0: {side: 'top'}
}
}
};
var chart = new google.charts.Line(document.getElementById('line_top_x'));
chart.draw(data, google.charts.Line.convertOptions(options));
}
</script>
</head>
<body>
<div id="line_top_x"></div>
</body>
</html>
* I need the to show 60 days of weather comparison in the line chart.