first i appologize i am a total noob and even more noob in js. I use to code in php.
首先我道歉我是一个总菜鸟,甚至更多的菜鸟在js。我用php编写代码。
I found this nice pie chart by google https://developers.google.com/chart/interactive/docs/gallery/piechart
我发现这个漂亮的饼图由谷歌https://developers.google.com/chart/interactive/docs/gallery/piechart
Im trying to built it with my own data (took from SQL with php and converted into php)
我试图用我自己的数据构建它(从SQL获取并转换为PHP)
My problem is that Js dosent seem to accept my variable as data for the table and i dont know why. Here is my code * I have 3 php variable converted to js ($num_ra1 to var countra1) , etc)
我的问题是Js dosent似乎接受我的变量作为表的数据,我不知道为什么。这是我的代码*我有3个php变量转换为js($ num_ra1到var countra1)等)
Please any help would be nice, i put a lots of time trying to make this work. Also the chrome tool send this error : Uncaught Error: Type mismatch. Value 17 does not match type number in column index 1 at gvjs_Ll
请任何帮助都很好,我花了很多时间试图做这项工作。 Chrome工具也会发送此错误:未捕获错误:类型不匹配。值17与gvjs_Ll的列索引1中的类型编号不匹配
Code : PHP and SQL
代码:PHP和SQL
<?php
$hostname='mysql:host=localhost;dbname=secure_login';
$username='root';
$password='root';
$tbl_2='econo1';
//create PDO connection
try {
$dbh = new PDO($hostname,$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// to get the data
$sql_ra1="SELECT COUNT(reduire_affecte1) AS ra1 FROM econo1 WHERE reduire_affecte1 != 0";
$sql_rf2="SELECT COUNT(reduire_facon2) AS rf2 FROM econo1 WHERE reduire_facon2 != 0";
$sql_ni3="SELECT COUNT(non_important3) AS ni3 FROM econo1 WHERE non_important3 != 0";
$result_ra1=$dbh->prepare($sql_ra1);
$result_ra1->execute();
$result_rf2=$dbh->prepare($sql_rf2);
$result_rf2->execute();
//$count_rf2=$result_rf2->fetch(PDO::FETCH_ASSOC); (pending)
$result_ni3=$dbh->prepare($sql_ni3);
$result_ni3->execute();
//$count_ni3=$result_ni3->fetch(PDO::FETCH_ASSOC); (pending)
}
//To hide the password if error
catch (Exception $e) {
die('Erreur : ' . $e->getMessage());
}
while ($count_ra1=$result_ra1->fetch(PDO::FETCH_OBJ))
{
$num_ra1=$count_ra1->ra1;
}
while ($count_rf2=$result_rf2->fetch(PDO::FETCH_OBJ))
{
$num_rf2=$count_rf2->rf2;
}
while ($count_ni3=$result_ni3->fetch(PDO::FETCH_OBJ))
{
$num_ni3=$count_ni3->ni3;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
// get the php result
var countra1="<?php echo json_encode($num_ra1); ?>";
var countrf2="<?php echo json_encode($num_rf2); ?>";
var countni3="<?php echo json_encode($num_ni3); ?>";
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Réponse');
data.addColumn('number', 'Choix');
data.addRows([
['reduire affecter', countra1],
['reduire façon', countrf2],
['non important', countni3],
]);
// Set chart options
var options = {'title':'votre opinion',
'width':400,
'height':300};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div" style="width:400; height:300"></div>
</body>
</html>
EDIT 2016-11-17 22h21 Solution found : i just want to update this for the other user that might have the problem. I find the solution, actually json_encode was thinking that the code was text and he put " " around the value of the php variable, and because i was myself already putting some "" around the php code, the number return by the php variable was between 2 double quote : " " 10 " " so js was seing it as a non numeric value. Solution : Remove my double quote. Now everything works perfectly
编辑2016-11-17 22h21发现解决方案:我只是想为可能有问题的其他用户更新此信息。我找到了解决方案,实际上json_encode认为代码是文本,他把“变量”的值放在了“周围”,因为我自己已经在PHP代码周围放了一些“”,php变量返回的数字是两个双引号之间:“”10“”所以js将其视为非数字值。解决方案:删除我的双引号。现在一切都很完美
var countra1=parseInt(<?php echo json_encode($num_ra1); ?>) || 0;
var countrf2=parseInt(<?php echo json_encode($num_rf2); ?>) || 0;
var countni3=parseInt(<?php echo json_encode($num_ni3); ?>) || 0;
And now my problem seem to be solved at 70%, the only piece missing is an error in the syntax. I guess its in the use of parseInt. The chrome devtool tells me its an ')' missing. If someone see something please let me know!
现在我的问题似乎以70%解决了,唯一缺少的是语法错误。我想它在使用parseInt时。 chrome devtool告诉我它'''缺失了。如果有人看到了什么,请告诉我!
2 个解决方案
#1
0
If you can provide always numbers - remove the quotes.
如果您可以提供始终数字 - 删除引号。
Use:
使用:
var countni3=<?php echo json_encode($num_ni3); ?>;
Or if you aren't sure:
或者如果您不确定:
var countni3=parseInt("<?php echo json_encode($num_ni3); ?>") || 0;
Example:
例:
var countra1=parseInt("<?php $num_ra1 = 10; echo json_encode($num_ra1); ?>") || 0;
var countrf2=parseInt("<?php $num_rf2 = 20; echo json_encode($num_rf2); ?>") || 0;
var countni3=parseInt("<?php $num_ni3 = 30; echo json_encode($num_ni3); ?>") || 0;
#2
0
I tried running your code by replacing countra1,countrf2,countni3 with numeric values and the code worked.
我尝试使用数值替换countra1,countrf2,countni3并运行代码来运行代码。
Please check if the values for countra1,countrf2,countni3 are numeric.
请检查countra1,countrf2,countni3的值是否为数字。
#1
0
If you can provide always numbers - remove the quotes.
如果您可以提供始终数字 - 删除引号。
Use:
使用:
var countni3=<?php echo json_encode($num_ni3); ?>;
Or if you aren't sure:
或者如果您不确定:
var countni3=parseInt("<?php echo json_encode($num_ni3); ?>") || 0;
Example:
例:
var countra1=parseInt("<?php $num_ra1 = 10; echo json_encode($num_ra1); ?>") || 0;
var countrf2=parseInt("<?php $num_rf2 = 20; echo json_encode($num_rf2); ?>") || 0;
var countni3=parseInt("<?php $num_ni3 = 30; echo json_encode($num_ni3); ?>") || 0;
#2
0
I tried running your code by replacing countra1,countrf2,countni3 with numeric values and the code worked.
我尝试使用数值替换countra1,countrf2,countni3并运行代码来运行代码。
Please check if the values for countra1,countrf2,countni3 are numeric.
请检查countra1,countrf2,countni3的值是否为数字。