PHP:如何将多个变量传递给数组? [英] PHP: How to pass multiple variables to an array?
问题描述
在谷歌图表API,图表中的数据被该行设定:
In the google chart api, the data for the chart is set by this line:
data.addRows([
['2004', 1000, 400],
['2005', 1170, 460],
['2006', 860, 580],
['2007', 1030, 540]
]);
我希望能够设置从数据这个数据在我的数据库。下面是我的数据库的图像:
I want to be able to set this data from data in my database. Below is an image of my database:
我要带 salePrice
和单价的所有值
以及对应于折线图上显示的值期间,他们创建的。
I want to take all values of salePrice
and unitPrice
and display the values on a line chart that corresponds to the period they were created.
下面是我的code:
<?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>
问题是我设置 $值
它是一个字符串,因此数据不正确输出的方式。有没有一种方法,我可以设置 $值
这样我就可以用它作为预期?
The problem is the way I am setting $value
it is a string and so the data is not outputted correctly. Is there a way I can set $value
so I can use it as intended?
在 $值
是呼应( $ currentStage
为价值3)这是输出:
[0,0,0],[1,65,35],['2',88,35]
When $value
is echoed ($currentStage
being the value 3) this is outputted:
['0',0, 0],['1',65, 35],['2',88, 35]
然而,当我查看源代码我得到:
However when I view source I am getting:
data.addRows(JSON.parse( ["['0',0, 0],['1',65, 35],['2',88, 35]"] ));
我需要摆脱的。
推荐答案
您必须分析你的JSON字符串:
You have to parse your JSON string :
data.addRows(JSON.parse( <?php echo json_encode($value); ?> ));
如果我没有记错应该解决您的问题。
If I'm not mistaken that should solve your problem.
修改其实我不知道我理解你为什么 $值
变量设置你的字符串; json_en code
是整点,你可以直接连接code对象或数组。
Edit In fact I'm not sure I understand why you're setting you $value
variable to a string; the whole point of json_encode
being that you can directly encode objects or arrays.
编辑2 :这里是我的看法,但我的PHP是不是更生锈
Edit 2 : Here's how I see it, but my php is more than rusty.
<?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];
}
修改3 我编辑了JS片段,我不知道我为什么离开周围的PHP标签的括号内。
Edit 3 I edited the js snippet, I don't know why I left the brackets around the php tags.
修改4 编辑PHP的code到工作的版本。
Edit 4 Edited the php code to the working version.
这篇关于PHP:如何将多个变量传递给数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!