如何将PHP变量从一个文件传递到另一个文件多次?

时间:2022-05-31 23:08:59

I have a highstock graph in a file called charts.php that reloads the graph data from a secondary file called data.php. Seeing as I have 5 series in my highstock graph I have to call the data.php file 5 times, each time using a different variable, but when I set the variable in my charts.php file it only uses the last set value of this variable. Is is possible to solve this, or is this just the way PHP works? Do I have to create 5 separate data.php files in order for this to work?

我在一个名为charts.php的文件中有一个highstock图表,它从名为data.php的辅助文件重新加载图形数据。看到我在我的highstock图中有5个系列我必须调用data.php文件5次,每次使用不同的变量,但是当我在我的charts.php文件中设置变量时,它只使用最后一个设置值变量。有可能解决这个问题,还是这只是PHP的工作方式?我是否必须创建5个单独的data.php文件才能使其正常工作?

Here's basically what my code looks like (showing 2 series to keep it short):

这基本上是我的代码的样子(显示2个系列以保持简短):

var chart = new Highcharts.StockChart({
    chart: {
        renderTo: 'chart'
    },

    title : { text : 'MyChart' },

    series : [
        {
            name : 'Unit 0',
            data : [],
            tooltip: { valueDecimals: 1, valueSuffix: ' kW' },
        },
        {
            name : 'Unit 1',
            data : [],
            tooltip: { valueDecimals: 1, valueSuffix: ' kW' },
        }
    ]
});

<?php $_SESSION["unit"] = 0; ?>

$.getJSON( "data.php", function (data) {
    chart.series[0].setData( data );
});

<?php $_SESSION["unit"] = 1; ?>

$.getJSON( "data.php", function (data) {
    chart.series[1].setData( data );
});

So the problem is that both series[0] and series[1] use the last value of my variable, which in this case is $_SESSION["unit"] = 1. Is the a way to make this work? Any better alternatives?

所以问题是系列[0]和系列[1]都使用我的变量的最后一个值,在这种情况下是$ _SESSION [“unit”] = 1.这是一种使这个工作的方法吗?有更好的选择吗?

Maybe if I could create data for all of the series in one file and somehow split that data and pass it to series[0] and series[1] separately, but I have no idea how to do that..

也许如果我可以在一个文件中创建所有系列的数据并以某种方式拆分该数据并将其分别传递给series [0]和series [1],但我不知道如何做到这一点..

Any help is appreciated.

任何帮助表示赞赏。

2 个解决方案

#1


1  

Maybe you could modify your data.php file, so instead of using $_SESSION["unit"] to determine which var to use, use $_GET["unit"] instead. Then, in your code above, it would be:

也许您可以修改data.php文件,因此不要使用$ _SESSION [“unit”]来确定要使用的var,而是使用$ _GET [“unit”]。然后,在上面的代码中,它将是:

$.getJSON( "data.php?unit=0", function (data) {
    chart.series[0].setData( data );
});

$.getJSON( "data.php?unit=1", function (data) {
    chart.series[1].setData( data );
});

#2


0  

It depends on your needs, if the $unit values are always the same I would just call data.php once and update all the graphs at once using the JS response.

这取决于您的需求,如果$ unit值始终相同,我只需调用data.php一次,并使用JS响应一次更新所有图形。

If $unit has a dynamic variable I would send it along with the $.getJSON function.

如果$ unit有一个动态变量,我会将它与$ .getJSON函数一起发送。

var data = {
  unit: $_GET['unit']
};

$.getJSON( "data.php", data, function (response) {
  updateGraph(graph, response);
});

For more on getJSON on the structure of the function, visit http://api.jquery.com/jquery.getjson/

有关该函数结构的getJSON的更多信息,请访问http://api.jquery.com/jquery.getjson/

#1


1  

Maybe you could modify your data.php file, so instead of using $_SESSION["unit"] to determine which var to use, use $_GET["unit"] instead. Then, in your code above, it would be:

也许您可以修改data.php文件,因此不要使用$ _SESSION [“unit”]来确定要使用的var,而是使用$ _GET [“unit”]。然后,在上面的代码中,它将是:

$.getJSON( "data.php?unit=0", function (data) {
    chart.series[0].setData( data );
});

$.getJSON( "data.php?unit=1", function (data) {
    chart.series[1].setData( data );
});

#2


0  

It depends on your needs, if the $unit values are always the same I would just call data.php once and update all the graphs at once using the JS response.

这取决于您的需求,如果$ unit值始终相同,我只需调用data.php一次,并使用JS响应一次更新所有图形。

If $unit has a dynamic variable I would send it along with the $.getJSON function.

如果$ unit有一个动态变量,我会将它与$ .getJSON函数一起发送。

var data = {
  unit: $_GET['unit']
};

$.getJSON( "data.php", data, function (response) {
  updateGraph(graph, response);
});

For more on getJSON on the structure of the function, visit http://api.jquery.com/jquery.getjson/

有关该函数结构的getJSON的更多信息,请访问http://api.jquery.com/jquery.getjson/