使用谷歌图表与PHP / mysqli - 无法让它工作 [英] Using Google charts with php/mysqli - cannot get it working
问题描述
我从这里读了很多很多的例子,出于某种原因,我只是不能让他们工作。
我从这里举了一个例子:
PHP MySQL Google Chart JSON - 完整的示例
并且正在使用PHP-MySQLi-JSON-Google Chart示例
某些原因,数据只是没有使用foreach方法从MySQL数据库中提取。我现在使用fetch_assoc将其更改为while循环,并且它已经拉取了数据并创建了正确的json格式。
当我加载页面时,虽然我只是空白页面。
我现在不知道为什么它不起作用。
以下是一些可能有所帮助的信息:
php 5.3.17
OpenSuse 12.3
我检查了apache日志,没有错误。任何想法我可以做什么来弄清楚这一点吗?
jsonTable:
{
cols :[
{label:Weekly Task,type:string},
{label:Percentage,type:number}
],
rows:[
{c:[{v:running},{v:30}]},
{c: [{v:jorunning},{v:30}]},
{c:[{v:job},{v:20}]} ,
{c:[{v:sleeping},{v:40}]},
{c:[{v:exercise}} ,{v:50}]},
{c:[{v:resting},{v:30}]}
]
}
以下是我的代码:
<?php
$ DB_NAME ='chart';
$ DB_HOST ='localhost';
$ DB_USER ='test';
$ DB_PASS ='123456';
$ mysqli = new mysqli($ DB_HOST,$ DB_USER,$ DB_PASS,$ DB_NAME);如果(mysqli_connect_errno()){
printf(连接失败:%s \ n,mysqli_connect_error());
exit();
}
$ query =select * from googlechart;
if($ result = $ mysqli-> query($ query)){
{
$ rows = array();
$ table = array();
$ table ['cols'] = array(
array('label'=>'Weekly Task','type'=>'string'),
array('label '=>'百分比','类型'=>'数字')
);
while($ row = $ result-> fetch_assoc()){
$ temp = array();
$ temp [] = array('v'=>(string)$ row ['weekly_activity']);
$ temp [] = array('v'=>(int)$ row ['percentage']);
$ rows [] = array('c'=> $ temp);
}
}
}
$表['rows'] = $ rows;
$ jsonTable = json_encode($ table);
?>
< html>
< head>
<! - 加载Ajax API - >
< script type =text / javascriptsrc =https://www.google.com/jsapi>< / script>
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js>< / script>
< script type =text / javascript>
//加载Visualization API和饼图包。
google.load('visualization','1',{'packages':['corechart']});
//设置回调以在加载Google Visualization API时运行。
google.setOnLoadCallback(drawChart);
函数drawChart(){
//从服务器加载的JSON数据中创建我们的数据表。
var data = new google.visualization.DataTable(<?= $ jsonTable?>);
var options = {
title:'My Weekly Plan',
is3D:'true',
width:800,
height:600
} ;
//实例化并绘制我们的图表,并传入一些选项。
//不要忘记检查你的div ID
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data,options);
}
< / script>
< / head>
< body>
<! - 这是容纳饼图的div - >
< div id =chart_div>< / div>
<?php echo $ jsonTable; ?>
< / body>
< / html>
以下是加载页面的源代码:
< HTML>
< head>
< script type =text / javascriptsrc =https://www.google.com/jsapi>< / script>
< script type =text / javascriptsrc =// ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js\"> < /脚本>
< script type =text / javascript>
//加载可视化API和饼图包。
google.load('visualization','1',{'packages':['corechart']});
//设置回调以在加载Google Visualization API时运行。
google.setOnLoadCallback(drawChart);
函数drawChart(){
//从服务器加载的JSON数据中创建我们的数据表。
var data = new google.visualization.DataTable(<?= $ jsonTable?>);
var options = {
title:'My Weekly Plan',
is3D:'true',
width:800,
height:600
} ;
//实例化并绘制我们的图表,并传入一些选项。
//不要忘记检查你的div ID
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data,options);
}
< / script>
< / head>
< body>
<! - 这是容纳饼图的div - >
< div id =chart_div>< / div>
< / body>
< / html>
我将其更改为:
<?php echo $ jsonTable; ?>
现在我更加困惑了,为什么SO上列出的所有例子都给出了,他们说他们工作。
I've read many many examples from here and for some reason I just cant get them to work.
I have taken the example from here:
PHP MySQL Google Chart JSON - Complete Example
and am using the PHP-MySQLi-JSON-Google Chart Example
For some reason the data was just not pulled from the mysql database using the foreach method. I have now changed this to a while loop using fetch_assoc and it has pulled the data and created the correct json format.
When I load the page though I just get a blank page.
I now have no idea as to why its not working.
Here's some info which might help: php 5.3.17 OpenSuse 12.3
I've checked the apache logs and there are no errors. Any ideas what else I can do to figure this out?
jsonTable:
{
"cols":[
{"label":"Weekly Task","type":"string"},
{"label":"Percentage","type":"number"}
],
"rows":[
{"c":[{"v":"running"},{"v":30}]},
{"c":[{"v":"jorunning"},{"v":30}]},
{"c":[{"v":"job"},{"v":20}]},
{"c":[{"v":"sleeping"},{"v":40}]},
{"c":[{"v":"exercise"},{"v":50}]},
{"c":[{"v":"resting"},{"v":30}]}
]
}
Here is my code:
<?php
$DB_NAME = 'chart';
$DB_HOST = 'localhost';
$DB_USER = 'test';
$DB_PASS = '123456';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "select * from googlechart";
if ($result = $mysqli->query($query)) {
{
$rows = array();
$table = array();
$table['cols'] = array(
array('label' => 'Weekly Task', 'type' => 'string'),
array('label' => 'Percentage', 'type' => 'number')
);
while ($row = $result->fetch_assoc()) {
$temp = array();
$temp[] = array('v' => (string) $row['weekly_activity']);
$temp[] = array('v' => (int) $row['percentage']);
$rows[] = array('c' => $temp);
}
}
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
?>
<html>
<head>
<!--Load the Ajax API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(<?=$jsonTable?>);
var options = {
title: 'My Weekly Plan',
is3D: 'true',
width: 800,
height: 600
};
// Instantiate and draw our chart, passing in some options.
// Do not forget to check your div ID
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<!--this is the div that will hold the pie chart-->
<div id="chart_div"></div>
<?php echo $jsonTable; ?>
</body>
</html>
Here is the source from a loaded page:
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(<?=$jsonTable?>);
var options = {
title: 'My Weekly Plan',
is3D: 'true',
width: 800,
height: 600
};
// Instantiate and draw our chart, passing in some options.
// Do not forget to check your div ID
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<!--this is the div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
I changed it to this:
<?php echo $jsonTable; ?>
I even more confused now as to why all the examples listed on SO are given and they say they work.
这篇关于使用谷歌图表与PHP / mysqli - 无法让它工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!