简体   繁体   English

通过jquery从php数组中获取单个字符串,并将其加载到html div中

[英]Get a single string out of a php array via jquery and load it inside a html div

I am trying to get the string "Name n" out of the php array $F[], which is randomised inside the function DataInit1(), and put it in a div inside my html file. 我正在尝试从php数组$ F []中获取字符串“ Name n”,该数组在函数DataInit1()中随机分配,并将其放在我的html文件中的div中。 However, the PHP file cannot be changed because the arrays are randomised and used to plot a series of graphs. 但是,PHP文件无法更改,因为数组是随机的并用于绘制一系列图形。

PHP file: PHP文件:

<?php
$Return = array();
$P = array();
$S = array();
$F = array();

$P[] = [0];
$S[] = [[0.00,111],[0.50,104.74],[1.00,91.29],[1.50,93.28],...];
$F[] = "Name 1";

$P[] = [0];
$S[] = [[0.00,199],[0.50,84.06],[1.00,82.43],[1.50,83.02],...];
$F[] = "Name 2";

for($i=0; $i<count($P); $i++)
{
$Return[] = $P[$i];
$Return[] = $S[$i];
$Return[] = $F[$i];
}
die(json_encode($Return));

?>

HTML file: HTML档案:

<div id="GRAPH">
    <div id="title"><h1 id='graphtitle'></h1></div>
       <h1 id='GraphNum'></h1>
    <div id="chart"></div>
        <h1 id='PointNum'></h1>
</div>

The string should be placed in the "ARRAY $F" as shown below in the JS file: 字符串应放在JS文件中的“ ARRAY $ F”中,如下所示:

function DataInit1()
{
$.ajaxSetup({cache: false, async: false});
$.getJSON("data1.php",
    function(Data1)
    {   
        SeriesList = [];
        CurrentGraph.Series = 0;
        for(var i=0; i<Data1.length; i+=3)
        {
            var P = Data1[i+0];
            var S = Data1[i+1];
            var F = Data1[i+2];
            var NewSeries = new SeriesClass(P,S,F);
            NewSeries.SeriesNumber = (i/3)+1;
            SeriesList.push(NewSeries);
        }
    }
);

for(var i=SeriesList.length-1; i>0; i--) 
{
    var j = Math.floor(Math.random() * (i+1));
    var x = SeriesList[i];
    SeriesList[i] = SeriesList[j];
    SeriesList[j] = x;
}
}

........

function Title()
{
    $("#title").show();
    $.ajaxSetup({cache: false, async: false});
    $.getJSON("data1.php",
        function(data)              
        {
            var name = ARRAY $F; 
        });
    $("#graphtitle").html(name);
}

Any other idea or suggestion on this issue will be very welcome. 在这个问题上任何其他想法或建议都将受到欢迎。 Thank you. 谢谢。

UPDATE: 更新:

Based on the suggestion by ironpilot. 根据ironpilot的建议。 Could the solution be something like this?: 解决方案可以是这样吗?:

function Title()
{
$("#title").show();
$.ajaxSetup({cache: false, async: false});
$.getJSON("CurrentGraph.Series",        
    function(data)                 
    {   
    var titleGraph = (json_decode($F, true));
    var name = data.titleGraph[0];  
    $("#graphtitle").html(name);
    });
}

You have a few issues in your code that you would need to correct before this is possible. 您的代码中有一些问题需要纠正,然后才能解决。 First let's look at your PHP code. 首先让我们看一下您的PHP代码。 It appears that you want to create a few parallel arrays and then return them to your front-end code. 看来您想创建一些并行数组,然后将它们返回到前端代码。 However, the way that you add each array into the $Return array makes predicting where the elements are nearly impossible. 但是,将每个数组添加到$Return数组的方式使得预测元素的位置几乎是不可能的。

Let's start by converting the $Return variable to an object of stdClass . 让我们首先将$Return变量转换为stdClass对象。 This will allow you to specify arbitrary properties on the object and make them accessible via a JavaScript object. 这将允许您在对象上指定任意属性,并使它们可通过JavaScript对象访问。

<?php
$Return = new stdClass();
$Return->P = array();
$Return->S = array();
$Return->F = array();
$P = array();
$S = array();
$F = array();

$P[] = [0];
$S[] = [[0.00,111],[0.50,104.74],[1.00,91.29],[1.50,93.28],...];
$F[] = "Name 1";

$P[] = [0];
$S[] = [[0.00,199],[0.50,84.06],[1.00,82.43],[1.50,83.02],...];
$F[] = "Name 2";

for($i=0; $i<count($P); $i++)
{
    $Return->P = $P[$i];
    $Return->S = $S[$i];
    $Return->F = $F[$i];
}
die(json_encode($Return));

?>

Now that we have a little more predictable object to work with we can correct the jQuery code: 现在我们有了更多可预测的对象,我们可以更正jQuery代码:

function Title()
{
    $("#title").show();
    $.ajaxSetup({cache: false, async: false});
    $.getJSON("data1.php",
        function(data)              
        {
            var name = data.F[0]; //Select the first element of the $F array.
            $("#graphtitle").html(name);
        });
}

Now that we have an object that is returned from PHP you can access the array of names specifically to get the value that you need with var name = data.F[0]; 现在我们有了从PHP返回的对象,您可以使用var name = data.F[0];专门访问名称数组以获取所需的值var name = data.F[0]; . Additional since this variable was declared with the var keyword inside the success function it's value would not be accessible outside of that function where it was set. 另外,由于此变量是使用成功函数内的var关键字声明的,因此在设置该函数的函数外部无法访问其值。

We also needed to move your selector for #graphtitle inside the jQuery success method. 我们还需要将您的#graphtitle选择器移动到jQuery success方法中。 Since this method is actually a promise and doesn't execute until the AJAX request completes, what would have happened is that the function to set the html content of the element you were selecting would have executed before you received a response from the server with the content to place. 由于此方法实际上是一个承诺,并且在AJAX请求完成之前不会执行,因此发生的情况是,在您收到服务器发出的带有响应的响应之前,用于设置所选择元素的html内容的函数将已经执行。要放置的内容。 This is called a Race Condition: https://en.wikipedia.org/wiki/Race_condition . 这称为竞赛条件: https : //en.wikipedia.org/wiki/Race_condition

I hope that helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM