Incerc sa realizez un chart pie dinamic (valori primite de la un dropdown) cu valori dintr-o baza de date.
M-am izbit de urmatoarea problema: dupa ce am inserat:
Cod: Selectaţi tot
if(isset($_POST['get_option'])){
$sch = $_POST['get_option'];
//...
}
Am o daza de date denumita test ce contine o tabela tbl_teste avand urmatoarele coloane: id, nume, sales, anul.
Am un fisier query_chart_pie.php ce returneaza valori sub forma aceasta: ['popescu',369],['alin',124],
Cod: Selectaţi tot
<?php
include "bd_cnx.php";
if(isset($_POST['get_option']))
{
// Interogare SQL
$sch = $_POST['get_option'];
$sql = "SELECT nume, sales
FROM tbl_teste
WHERE anul = '$sch' ";
// Executa interogarea si retine datele returnate
$result = $conn->query($sql);
// daca $result contine cel putin un rand
if ($result->num_rows > 0) {
// afiseaza datele din fiecare rand din $result
while($row = $result->fetch_assoc()) {
echo "['".$row['nume']."',".$row['sales']."],";
}
} else {
echo "0 results";
}
// Se inchide conexiunea
$conn->close();
}
else {
print "nu exista valori in post";
}
?>
Cod: Selectaţi tot
<!doctype html>
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="jQuery.js"></script>
<script type="text/javascript">
function fetch_select(val)
{
$.ajax({
type: 'post',
url: 'query_chart_pie.php',
data: {
get_option:val
},
success: function (response) {
document.getElementById("new_select").innerHTML=response;
}
});
}
</script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
<?php include "query_chart_pie.php" ?>
]);
var options = {
title: 'My Daily Activities'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
</head>
<body style="background:gray;">
<div id="piechart" style="width: 900px; height: 500px;"></div>
<select onchange="fetch_select(this.value);">
<?php
$last_year = date ("Y");
for( $i = 2004; $i <= $last_year; $i++ ) {
$selected = ($last_year == $i) ? 'selected="selected"' : '';
echo "<option $selected value='$i'>$i</option>\n";
};
?>
</select>
<div id="new_select"><?php include"query_chart_pie.php" ?></div>
</body>
</html>
Unde gresesc de nu imi apare chart-ul (nu imi apare nici macar titlul, de legenda si valori nu mai vorbesc)
Multumesc!