【发布时间】:2023-03-23 08:23:02
【问题描述】:
我正在尝试使用 php echo 显示数据库中的数据 此代码显示一个图表,我正在尝试查看是否可以通过 echo 更改数据(将 350 值更改为其他值)。我在下面的示例代码中尝试了它,但它不起作用。有什么问题?
这一行有 php var raw_data = [['Website', , 73, 104, 129, 146, 176, 139, 149, 218, 194, 96, 253],现在我还没有从 mysql 中提取数据一旦确定我可以使用 echo 操作数据,我打算稍后再做
<script>
/*
* This script is dedicated to building and refreshing the demo chart
* Remove if not needed
*/
// Demo chart
var chartInit = false,
drawVisitorsChart = function()
{
// Create our data table.
var data = new google.visualization.DataTable();
var raw_data = [['Website', <?php echo '350';?>, 73, 104, 129, 146, 176, 139, 149, 218, 194, 96, 253],
['Shop', 82, 77, 98, 94, 105, 81, 104, 104, 92, 83, 107, 91],
['Forum', 50, 39, 39, 41, 47, 49, 59, 59, 52, 64, 59, 51],
['Others', 45, 35, 35, 39, 53, 76, 56, 59, 48, 40, 48, 21]];
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
data.addColumn('string', 'Month');
for (var i = 0; i < raw_data.length; ++i)
{
data.addColumn('number', raw_data[i][0]);
}
data.addRows(months.length);
for (var j = 0; j < months.length; ++j)
{
data.setValue(j, 0, months[j]);
}
for (var i = 0; i < raw_data.length; ++i)
{
for (var j = 1; j < raw_data[i].length; ++j)
{
data.setValue(j-1, i+1, raw_data[i][j]);
}
}
// Create and draw the visualization.
// Learn more on configuration for the LineChart: http://code.google.com/apis/chart/interactive/docs/gallery/linechart.html
var div = $('#demo-chart'),
divWidth = div.width();
new google.visualization.LineChart(div.get(0)).draw(data, {
title: 'Monthly unique visitors count',
width: divWidth,
height: $.template.mediaQuery.is('mobile') ? 180 : 265,
legend: 'right',
yAxis: {title: '(thousands)'},
backgroundColor: ($.template.ie7 || $.template.ie8) ? '#494C50' : 'transparent', // IE8 and lower do not support transparency
legendTextStyle: { color: 'white' },
titleTextStyle: { color: 'white' },
hAxis: {
textStyle: { color: 'white' }
},
vAxis: {
textStyle: { color: 'white' },
baselineColor: '#666666'
},
chartArea: {
top: 35,
left: 30,
width: divWidth-40
},
legend: 'bottom'
});
// Message only when resizing
if (chartInit)
{
notify('Chart resized', 'The width change event has been triggered.', {
icon: 'img/demo/icon.png'
});
}
// Ready
chartInit = true;
};
// 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(drawVisitorsChart);
// Watch for block resizing
$('#demo-chart').widthchange(drawVisitorsChart);
// Respond.js hook (media query polyfill)
$(document).on('respond-ready', drawVisitorsChart);
</script>
【问题讨论】:
-
我花了一段时间才找到它,但我删除了最初的评论。当你说“它不起作用”时,你能说得更具体些吗?
-
@RamRaider 我尝试将值从 350 更改为其他值,但一旦我这样做,图表就会消失。
-
但
350是否真的被写为源代码中的变量(查看源代码)?我建议你删除'350'周围的引号 - 你需要一个数字,你用 PHP 编写的在技术上是一个字符串 ~ 即<?php echo 350;?>应该这样做 -
如果我使用 php 代码,什么都不会写在源代码中。我从 350 左右删除了“”,但仍然没有显示图表。如果我删除 php 代码,图表会显示备份
-
控制台是否出现任何错误?
标签: javascript php mysql database echo