gpt4 book ai didi

javascript - 使用MySQL数据和PHP获取的Google图表?

转载 作者:行者123 更新时间:2023-11-30 22:35:16 26 4
gpt4 key购买 nike

我想使用Google的图表API和通过MySQL获取的数据来创建动态条形图。我正在使用PHP创建页面。我能够用硬编码的值创建一个简单的图表,而没有任何问题。现在我正尝试使用一些MySQL数据,但是没有运气。此代码生成一个空白屏幕。

       <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(<?=$jsonTable1?>);
var options = {
title: '',
is3D: 'true',
width: 230,
height:145,
animation:{
duration: 100,
easing: 'out',
}
};
// Instantiate and draw our chart, passing in some options.
// Do not forget to check your div ID
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div1'));
chart.draw(data, options);
}
</script>




<html>
<body>
<form method="post" action="">
<input type="text" name="regi" >
<br />
<input type="submit" name="submit" >
</form>

<div id="chart_div1"></div>


//php code

<?php

$DB_NAME = 'add';
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';

$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

if(isset($_POST['submit']))
{
$regi=$_POST['regi'];
$sth = $mysqli->query('SELECT * FROM overall WHERE year="$regi"');



$rows = array();
$table = array();
$table['cols'] = array(

array('label' => 'year', 'type' => "string"),

array('label' => 'Final CSE-1', 'type' => 'number')




);
foreach($sth as $r) {

$temp = array();
$temp[] = array('v' => (string) $r['subjectcode']);

// Values of each slice
$temp[] = array('v' => (int) $r['percentage']);
$rows[] = array('c' => $temp);

}

$table['rows'] = $rows;
$jsonTable1 = json_encode($table);
//echo $jsonTable;
}
?>
</body>
</html>

最佳答案

如果您使用的是chrome,请按f12键并检查控制台错误。这将为我们提供有关失败原因的更多信息。因为您可能在数据库中有一些损坏的值,或类似的东西。因为除此之外,它看起来还不错。

还请记住关于SQL Injection的信息。

关于javascript - 使用MySQL数据和PHP获取的Google图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32734197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com