0
Good morning, I’m using a Google Hart to create a dashboard, only that it’s only bringing me a result my loop isn’t working, someone could help me see what’s wrong ?
<?php
include("conecta.php");
$consumo_atual = mysql_query("SELECT * FROM Arduino.Leitura");
while ($linha = mysql_fetch_array($consumo_atual)) {
$v_data =$linha["Data"];
$v_consumo =$linha["Consumo"];
}
?>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['DATA', 'COSUMO'],
['<?php echo "$v_data"; ?>', <?php echo "$v_consumo"; ?>]
]);
var options = {
title: 'Perfomace de Consumo',
hAxis: {title: 'Consumo Atual', titleTextStyle: {color: '#333'}},
vAxis: {minValue: 0}
};
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
I need to bring all the results because this Chart and line.
Follows amendment:
<?php
include("conecta.php");
$consumo_atual = mysql_query("SELECT * FROM Leitura");
$v_data_consumo[] = array('DATA', 'CONSUMO');
while ($linha = mysql_fetch_array($consumo_atual)) {
$v_data_consumo[] = array($linha["Data"], $linha["Consumo"]);
}
?>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([<?php echo json_encode($v_data_consumo); ?>]);
var options = {
title: 'Perfomace de Consumo',
hAxis: {title: 'Consumo Atual', titleTextStyle: {color: '#333'}},
vAxis: {minValue: 0}
};
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
Is worth a penny: Why should we not use mysql type functions_*?, so you will understand this mistake
deprecated
– Ivan Ferrer