PHP: How to pass multiple variables to an array?

2019-05-29 10:50发布

问题:

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:

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 "".

回答1:

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.