Reputation: 11
I'm trying to draw a pie chart using PHP and MySQL, but I don't see anything, any error just a blank page, I don't know where is the problem exactly, my query is correct. Any help, any suggestion! Thank you. This is my code that I'm using:
<?php
$dsn='mysql:host=localhost;dbname=tp3_php';
$user='root';
$pass='';
try {
$bdd = new PDO($dsn,$user,$pass);
} catch (Exception $e) {
die('Erreur : ' . $e->getMessage());
}
$sql="
SELECT Nom_matiere
, COUNT(ID_etudiant)
FROM note
, matiere
WHERE note>=12
and matiere.Num_matiere = note.Num_matiere
GROUP
BY note.Num_matiere
";
$sth = $bdd->query($sql);
?>
<html>
<head>
<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([
['Nom_matiere', 'taux de reussis'],
<?php
while ($result=$sth->fetchAll())
{
echo "['".$results['Nom_matiere']."',".$results['COUNT(ID_etudiant)']."],";
}
?>
]);
var options = {
title: 'Taux de réussite des étudiants par module'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="piechart" style="width: 900px; height: 500px;"></div>
</body>
</html>
Upvotes: 0
Views: 824
Reputation: 11
It worked for me: I added this line $result=$sth->fetchAll();
and I added a loop foreach
, the code will be like this:
['Nom_matiere', 'taux de reussis'],
<?php
$result=$sth->fetchAll();
foreach ($result as $row) {
echo "['".$row['Nom_matiere']."',".$row['COUNT(ID_etudiant)']."],";
}
?>
Upvotes: 1