gpt4 book ai didi

php - 如何在 Google Visualization API 中使用 PHP 变量来定义数据行

转载 作者:行者123 更新时间:2023-11-28 23:30:16 24 4
gpt4 key购买 nike

第一次使用 Google 可视化 API,我尝试在函数 createChart() 中使用 PHP 变量定义行数据。正在使用的代码返回以下错误

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in

这是我正在使用的代码

function createChart() {

//create data table object
var dataTable = new google.visualization.DataTable();

//define columns
dataTable.addColumn('string','Name');
dataTable.addColumn('number', 'Total');

<?php
$year=1;
$subjectID=1;

$result = mysqli_query($connection, "select * from fullresult where year='{$year}' and subject_id='{$subjectID}' order by firstname");
while ($students = mysqli_fetch_assoc($result)) {
?>

//define rows of data
//this is the part of the code returning the error
<?php echo "dataTable.addRow(['$students[\"firstname\"]',$students[\"total\"]])"; } ?>

//instantiate our chart object
var chart = new google.visualization.ColumnChart (document.getElementById('chart'));

//define options for visualization
var options = {width: 400, height: 240, is3D: true, title: 'Student Performance Summary'};

//draw our chart
chart.draw(dataTable, options);

}

很遗憾,我没能找到与此类似的任何问题。那么如何有效地使用变量来定义数据行而不出错呢?

欢迎提出意见和更正,我将不胜感激,因为我期待着了解更多这方面的知识。

最佳答案

认为是对转义字符的轰炸

尝试这样的事情......

<?php echo "dataTable.addRow(['".$students['firstname']."',".$students['total']."]);"; } ?>

一定要在 JavaScript 语句中包含 ;

关于php - 如何在 Google Visualization API 中使用 PHP 变量来定义数据行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37555100/

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