I'm new to php, I'm trying to stock my chart with my bank information.
I made some adjustments to your code:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Highcharts Example</title>
<style type="text/css">
</style>
</head>
<body>
<?php
include 'conexao.php';
$resTotal = $con->query("SELECT * FROM aluno");
$cnt = $resTotal->rowCount();
$resProf1 = $con->query("SELECT * FROM aluno where professor='lobato'");
$cntProf1 = $resProf1->rowCount();
$totalProf1 = $cntProf1 * 100 /$cnt;
$resProf2= $con->query("SELECT * FROM aluno where professor='willys'");
$cntProf2= $resProf2->rowCount();
$totalProf2 = $cntProf2 * 100 /$cnt;
$resProf3= $con->query("SELECT * FROM aluno where professor='allan'");
$cntProf3 = $resProf3->rowCount();
$totalProf3 = $cntProf3 * 100 /$cnt;
while($filaProf1 = $resProf1->fetch(PDO::FETCH_ASSOC)){
$prof1 = "{ name: '".$filaProf1['professor']."', y:".$totalProf1."},";
}
while($filaProf2 = $resProf2->fetch(PDO::FETCH_ASSOC)){
$prof2 = "{ name: '".$filaProf2['professor']."', y:".$totalProf2."},";
}
while($filaProf3 = $resProf3->fetch(PDO::FETCH_ASSOC)){
$prof3 = "{ name: '".$filaProf3['professor']."', y:".$totalProf3."},";
}
var_dump($prof1);
?>
<script src="highcharts/code/highcharts.js"></script>
<script src="highcharts/code/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>
<script type="text/javascript">
Highcharts.chart('container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Browser market shares January, 2015 to May, 2015'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
series: [{
name: 'Brands',
colorByPoint: true,
data: [
<?php
echo isset($prof1)? $prof1 : null;
echo isset($prof2)? $prof2 : null;
echo isset($prof3)? $prof3 : null;
?>
]
}]
});
</script>
</body>
</html>
I changed the fetch_array
by fetch(PDO::FETCH_ASSOC)
gave a var_dump of the result and it seems that it is ok.
I've also changed the line:
series: [{
name: 'Brands',
colorByPoint: true,
data: [
<?
echo
I was missing <?php
and added the following validation:
<?php
echo isset($prof1)? $prof1 : null;
echo isset($prof2)? $prof2 : null;
echo isset($prof3)? $prof3 : null;
?>
This ternary validates if the variable exists in the scope, because if it does not find any results for $prof2
or $prof1
or $prof3
the line that was could cause error in php, trying to use a variable that does not exist , because it is declared only within the while, that is, only if it finds any records in the query.
I saw here that you have an import error of the graphics file, I do not know if you are using files from your machine, if the problem persists send the import files, then we can help you better.
I hope I have helped:)
obs: I considered the following bank:
CREATE TABLE 'dbpesquisa'.'aluno' (
'idaluno' INT NOT NULL AUTO_INCREMENT,
'nome' VARCHAR(45) NULL,
'sobrenome' VARCHAR(45) NULL,
'curso' VARCHAR(45) NULL,
'professor' VARCHAR(45) NULL,
PRIMARY KEY ('idaluno'));