PHP:如何将多个变量传递给数组?
在google图表api中,图表的数据由以下行设置:PHP:如何将多个变量传递给数组?,php,arrays,google-visualization,Php,Arrays,Google Visualization,在google图表api中,图表的数据由以下行设置: data.addRows([ ['2004', 1000, 400], ['2005', 1170, 460], ['2006', 860, 580], ['2007', 1030, 540] ]); 我希望能够从数据库中的数据设置此数据。下面是我的数据库的图像: 我想获取salePrice和unitPrice的所有值,并将这些值显示在与创建它们的时间段相对应的折线图上 这是我的密码: <?php includ
data.addRows([
['2004', 1000, 400],
['2005', 1170, 460],
['2006', 860, 580],
['2007', 1030, 540]
]);
我希望能够从数据库中的数据设置此数据。下面是我的数据库的图像:
我想获取salePrice
和unitPrice
的所有值,并将这些值显示在与创建它们的时间段相对应的折线图上
这是我的密码:
<?php
include("getteam.php");
$saleprice = mysql_query("
SELECT `outputValue` FROM `output` WHERE `teamID` = '$teamID' && `outputType` = 'salePrice'
")or die($saleprice."<br/><br/>".mysql_error());
// set ID's = to a variable and now get Outputs for each variable(teamID)
$salepriceNumR = mysql_num_rows($saleprice);
$sPrice = array();
$i="0";
while ($i<$salepriceNumR && $row = mysql_fetch_assoc($saleprice))
{
$sPrice[$i] = $row['outputValue'];
$i++;
}
$unitprice = mysql_query("
SELECT `outputValue` FROM `output` WHERE `teamID` = '$teamID' && `outputType` = 'unitPrice'
")or die($unitprice."<br/><br/>".mysql_error());
// set ID's = to a variable and now get Outputs for each variable(teamID)
$unitpriceNumR = mysql_num_rows($unitprice);
$uPrice = array();
$i="0";
while ($i<$unitpriceNumR && $row = mysql_fetch_assoc($unitprice))
{
$uPrice[$i] = $row['outputValue'];
$i++;
}
$chartrow = array();
for ($i = 0; $i < $unitpriceNumR; $i++ )
{
$chartrow[$i] = "['".$i."',".$sPrice[$i].", ".$uPrice[$i]."]";
}
switch ($currentStage) {
case "0":
case "1":
$value = $chartrow[0];
break;
case "2":
$value = $chartrow[0].",".$chartrow[1];
break;
case "3":
$value = $chartrow[0].",".$chartrow[1].",".$chartrow[2];
break;
default:
$value = $chartrow[0];
// You should have some default value, seriously!!!
}
?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Sales');
data.addColumn('number', 'Expenses');
data.addRows(JSON.parse( [<?php echo json_encode($value); ?>] ));
var options = {
width: 400, height: 240,
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<div id="chart_div"></div>
我需要去掉“.”您必须解析JSON字符串:
data.addRows(JSON.parse( <?php echo json_encode($value); ?> ));
edit3我编辑了js代码段,我不知道为什么我在php标记周围留下了括号
edit4将php代码编辑为工作版本。我已经尝试过了,但仍然一无所获。当我查看源代码时,我得到了这个data.addRows(JSON.parse([“['0',0,0],'1',65,35],'2',88,35]”);“不应该在那里。我使用chrome,IE中查看源代码的结果相同。不要下载firefox,所以无法检查。好的,我这样做的原因是:我只是对如何设置
$value
感到困惑。我想你应该将$value
本身设置为一个数组。当我查看源代码时,我得到数据。addRows(JSON.parse([”['0',0,0],'1',65,35],'2',88,35]“]);我需要摆脱。有关详细信息,请参阅此链接:
data.addRows(JSON.parse( <?php echo json_encode($value); ?> ));
<?php
$chartrow = array();
for ($i = 0; $i < $unitpriceNumR; $i++ )
{
$chartrow[$i] = array( (string)$i, (int)$sPrice[$i], (int)$uPrice[$i] );
}
switch ($currentStage) {
case "0":
case "1":
$value = $chartrow[0];
break;
case "2":
$value = array($chartrow[0], $chartrow[1]);
break;
case "3":
$value = array($chartrow[0], $chartrow[1], $chartrow[2]);
break;
default:
$value = $chartrow[0];
}