gpt4 book ai didi

php - 谷歌可视化 API + SQL

转载 作者:行者123 更新时间:2023-11-29 15:07:32 26 4
gpt4 key购买 nike

我想在 MySql 数据库中创建一些数据的 GViz,但遇到了问题。

这是迄今为止的来源:

<?php 
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error
connecting to mysql');
mysql_select_db($dbname);
$int_y_pos = -1;
$int_y_step_small = 1;
$sql = "SELECT * from table')";
$sql = mysql_query($sql);
$rownum = mysql_num_rows($sql);
?>
<html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi"></
script>
<script type="text/javascript">
google.load("visualization", "1", {packages:
["table"]});
google.setOnLoadCallback(drawData);
function drawTable() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'column1');
data.addColumn('string', 'column2');
<?php
echo " data.addRows($rownum);\n";
while($row = mysql_fetch_assoc($sql)) {
$int_y_pos += $int_y_step_small;
echo " data.setValue(" . $int_y_pos . ", 0, new column1(" .
$row['column1'] . "));\n";
echo " data.setValue(" . $int_y_pos . ", 0, new column2(" .
$row['column2'] . "));\n";
}

?>
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(data, {showRowNumber: true});

google.visualization.events.addListener(table, 'select', function() {
var row = table.getSelection()[0].row;
alert('You selected ' + data.getValue(row, 0));
});
}
</script>
</head>
<body>
<div id="table_div" style="width: 940px; height: 240px;"></div>
</body>
</html>

任何帮助或以前集成的示例都会很棒。

谢谢

加雷斯

最佳答案

这对我有用:1.为表格设置标题

<?php
$dataTable = array(
'cols' => array(
// each column needs an entry here, like this:
array('type' => 'string', 'label' => 'Col1'),
array('type' => 'string', 'label' => 'Col2'),
array('type' => 'string', 'label' => 'Col3')
)
);

然后在 while 中设置行

while($row = mysql_fetch_array($result)) {
$dataTable['rows'][] = array(
'c' => array(
array('v' => $row['DB-col1']),
array('v' => $row['DB-col2']),
array('v' => $row['DB-col3'])
)
);

编码为 JSON:

        $json = json_encode($dataTable);?>

然后加载容器并将其显示为表格:

  function drawTable() {
var data = new google.visualization.DataTable(<?php echo $json; ?>);
var table = new google.visualization.Table(document.getElementById('your-div-id'));
table.draw(data);
}

关于php - 谷歌可视化 API + SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1692591/

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