PHP:如何将多个变量传递给数组?

发布于 2024-12-20 16:27:20 字数 3174 浏览 0 评论 0原文

在 google 图表 api 中,图表的数据由这一行设置:

data.addRows([
  ['2004', 1000, 400],
  ['2005', 1170, 460],
  ['2006',  860, 580],
  ['2007', 1030, 540]
]);

我希望能够从数据库中的数据设置此数据。下面是我的数据库的图片: 在此处输入图像描述

我想获取 salePriceunitPrice 并在折线图上显示与创建时间段相对应的值。

这是我的代码:

<?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>

问题是我设置 $value 的方式,它是一个字符串,因此数据无法正确输出。有没有办法设置 $value 以便我可以按预期使用它?

$value 被回显时($currentStage 为值 3),将输出以下内容: ['0',0, 0],['1',65, 35],['2',88, 35]

但是,当我查看源代码时,我得到:

 data.addRows(JSON.parse( ["['0',0, 0],['1',65, 35],['2',88, 35]"] )); 

我需要去掉“”。

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:
enter image description here

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.

Here is my 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?

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]"] )); 

I need to get rid of the "".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦境 2024-12-27 16:27:20

你必须解析你的 JSON 字符串:

data.addRows(JSON.parse( <?php echo json_encode($value); ?> ));  

如果我没有记错的话,这应该可以解决你的问题。

编辑 事实上,我不确定我是否理解为什么将 $value 变量设置为字符串; json_encode 的全部意义在于您可以直接对对象或数组进行编码。

编辑 2 :这是我的看法,但我的 php 不仅仅是生锈了。

<?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标签周围留下了括号。

编辑 4 将 php 代码编辑为工作版本。

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.

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.

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];  
}                

Edit 3 I edited the js snippet, I don't know why I left the brackets around the php tags.

Edit 4 Edited the php code to the working version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文