简体   繁体   English

显示数组中的数据

[英]Display data from an array

In index.php page i have a script that fetches data from demo.php and displays the result in a div. 在index.php页面中,我有一个脚本,可从demo.php获取数据并在div中显示结果。

<div class="leftbox">
    <?php
        echo "<div id='proddisplay'>";
        echo "</div>";
    ?>
</div>

var onSubmit = function(e) {
    var txtbox = $('#txt').val();
    var hiddenTxt = $('#hidden').val();
    $.ajax({
        type: 'post',
        url: 'demo.php',
        data: {
            txt: txtbox,
            hidden: hiddenTxt
        },
        cache: false,
        success: function(returndata) {
            $('#proddisplay').html(returndata);
            console.log(returndata);
        },
        error: function() {
            console.error('Failed to process ajax !');
        }
    });
};

in demo.php script i get the result from 在demo.php脚本中我得到了结果

print_r($result);

the result that i get in the div is 我进入div的结果是

Array ( [0] => 1st value [1] => 2nd value [2] => 3rd value [3] => 4th value )

I wish to get individial data from this array so that i can use each data seperately and wherever i want in the index.php page, but i am not able to do so. 我希望从此数组中获取个人数据,以便我可以分别使用每个数据以及在index.php页面中我想使用的任何位置,但是我无法这样做。 can anyone tell how can i fetch individual values from array for index page 谁能告诉我如何从索引页面的数组中获取单个值

PS: i have an option to display the format in demo page in the form of result[0] and so on for every index value and call it as it is in the index page but that bounds me with the css. PS:我可以选择在演示页中以result [0]的形式显示格式,依此类推,然后为每个索引值调用它,就可以在索引页中按原样调用它,但这使CSS受到了限制。 I wish to use any css in index page and just call the values in index page. 我希望在索引页面中使用任何CSS并仅在索引页面中调用值。

I tried using result[0] in index page but it displayed no result i tried assigning the values of an array to a variable and then call these variables to index page but that also didn't displayed any result 我尝试在索引页面中使用result [0],但未显示任何结果,我尝试将数组的值分配给变量,然后将这些变量调用到索引页面,但也未显示任何结果

convert your array to json 将您的数组转换为json

echo json_encode($result);

in your script 在您的脚本中

var onSubmit = function(e) {
  var txtbox = $('#txt').val();
  var hiddenTxt = $('#hidden').val();
  $.ajax({
    type: 'post',
    url: 'test2.php',
    dataType: 'json',
    data: {
      txt: txtbox,
      hidden: hiddenTxt
    },
    cache: false,
    success: function(returndata) {
         $('#first').html(returndata[0]);
          $('#second').html(returndata[1]);
           $('#third').html(returndata[2]);
            $('#fourth').html(returndata[3]);
         console.log(returndata[0]);
    },
    error: function() { 
      console.error('Failed to process ajax !');
    }
  });
};

first,second,third,fourth will be the id of the div or the area where you wish to display the result 第一,第二,第三,第四将是div的ID或您希望显示结果的区域

The simplest way to loop through values in an array is to use foreach . foreach数组中值的最简单方法是使用foreach

foreach 前言

foreach($result as $value){
    print($value);
}

http://php.net/manual/en/control-structures.foreach.php http://php.net/manual/en/control-structures.foreach.php

http://www.w3schools.com/php/php_looping_for.asp http://www.w3schools.com/php/php_looping_for.asp

Either access each value individually - for example $result[0] . 要么单独访问每个值,例如$result[0] Or if you want to loop over the entire array you can use foreach 或者,如果您想遍历整个数组,可以使用foreach

Example: 例:

foreach($result as $val){
   //do something with $val
}

PHP Arrays PHP数组

Simply assign values of indexes to variables like below: 只需为变量分配索引值,如下所示:

$first_value = $result[0];
$second_value = $result[1];
$third_value = $result[2];
$fourth_value = $result[3];

and so on in same sequence. 依此类推。

As you're using Ajax to get the data, you need to pass the array back as a JSON string. 使用Ajax获取数据时,需要将数组作为JSON字符串传递回去。 Add another parameter, dataType: 'JSON' to your Ajax call, this tells jQuery you're expecting a JSON string as a response and it'll automatically convert the JSON into an array for you when passing the result to the success function. 在您的Ajax调用中添加另一个参数dataType: 'JSON' ,这告诉jQuery您期望将JSON字符串作为响应,并且在将结果传递给成功函数时,它将自动将JSON转换为数组。

Then in your data.php value you need to run echo json_encode($result); 然后在data.php值中,您需要运行echo json_encode($result); to pass result back to the Ajax success function as a JSON string. 将结果作为JSON字符串传递回Ajax成功函数。 jQuery converts it back from a JSON string to an array for us (that's why we add the dataType ). jQuery为我们将其从JSON字符串转换回数组(这就是我们添加dataType的原因)。 Then in your Ajax success function you console.log(resultdata.0) you'll get value1, and console.log(resultdata.1) will give you value2. 然后,在Ajax成功函数中, console.log(resultdata.0)将获得value1, console.log(resultdata.1)将给您value2。

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

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