简体   繁体   English

PHP和AJAX:如何在PHP while循环中显示json_encode数据?

[英]PHP and AJAX: How can I display json_encode data in PHP while loop?

I am very new to using AJAX and passing data with json_encode. 我对使用AJAX和使用json_encode传递数据非常陌生。 I have this "aht" button when clicked it will send an AJAX request to my show_aht.php script, which will run a query. 单击该按钮时,我将具有一个“ aht”按钮,它将向我的show_aht.php脚本发送AJAX请求,该脚本将运行查询。 I want to save the results and display it to my map.php. 我想保存结果并将其显示到我的map.php中。

Problem: 问题:

In my map.php I have a while loop that outputs square(desk) DIVs with another DIV(station) when clicked that displays content inside of it. 在我的map.php中,我有一个while循环,当单击该循环时输出带有另一个DIV(station)的Square(desk)DIV,并在其中显示内容。 Here is the fiddle so you may understand . 这是小提琴,所以您可能会明白 I want the results of show_aht.php "TIME" to be displayed in the DIVs being produced by my WHILE LOOP in map.php. 我希望show_aht.php“ TIME”的结果显示在我的WHILE LOOP在map.php中生成的DIV中。

How is it possible to do this? 这怎么可能呢? I know that AJAX and PHP cannot interact with eachother and thats why I need help. 我知道AJAX和PHP无法相互交互,这就是为什么我需要帮助。 If this can't be done, how else can I display the TIME from show_aht.php to their corresponding usernames on each DIV being output? 如果无法做到这一点,那么在输出的每个DIV上,我还能如何显示show_aht.php中的TIME到其相应的用户名? I have around 200 of them being displayed. 我有大约200个正在显示。

Thanks in advance. 提前致谢。

map.php (only showing the last line of the while loop, outputs all DIVs) map.php(仅显示while循环的最后一行,输出所有DIV)

//desk DIV
 while(somequery){
    ....
    echo '<div class="' . $class . '" data-rel="' . $id . '" style="left:' . $x_pos . 'px;top:' . $y_pos.'px;">' . $sta_num . '</div>' . "\n";
}//end while

//station DIV
while(some query){
.....
   echo '<div class="station_info_" id="station_info_' . $id . '" style="left:' . $x_pos . 'px;top:'                             .$y_pos . 'px;"><p class="numb">User:' . $user .'<br>Station:' . $hostname . '<br>Pod:' . $sta_num .     '<br>Section:' . $sec_name . '<br>State:' . $state .'<br></p></div>' . "\n";
}//end while

map.php (AJAX part) map.php(AJAX部分)

<div id="aht"><!--aht button--> 
    <button id="aht_button">AHT</button>    
</div><!--aht button-->

<script type="text/javascript">
            $(document).ready(function() {
                $('#aht').click(function(){
                    $.ajax({
                    type:"POST",
                    url : "show_aht.php",
                    data: , // pass data here
                    dataType: 'json',
                    success : function(data){

                    }//end success
                });//end ajax
              });//end click
            });//end rdy
        </script>

show_aht.php (showing the loop and part I where I want the data to be returned) show_aht.php(显示循环和第一部分,我希望将数据返回到哪里)

foreach($memo_data as $v){
        foreach($user_data as $m){
            if($v['memo_code'] == $m){
                echo " User: " .$m. " Time: " . $v['avg_handle_time'] . "<br>";
            }
            elseif( $v['memo_code'] != $m){
                echo  "User: " . $m . " Time: N/A <br>";
            }
        }
    }

Don't output anything except one valid json string. 除了一个有效的json字符串,不输出任何东西。 In your case you do that by replacing the echo 's with building an array (or object...) and output that at the very end: 在您的情况下,您可以通过将echo替换为构建数组(或object ...)并在最后输出来实现:

For example: 例如:

$data = array();
foreach($memo_data as $v){
        foreach($user_data as $m){
            if($v['memo_code'] == $m){
                $data[] = " User: " .$m. " Time: " . $v['avg_handle_time'] . "<br>";
            }
            elseif( $v['memo_code'] != $m){
                $data[] = "User: " . $m . " Time: N/A <br>";
            }
        }
    }

// Output your json string
echo json_encode($data);

Note that I have simply replaced your echos with an assignment but you could also add arrays in your array to return just the data parts and process that afterwards in javascript. 请注意,我只是用一个赋值替换了您的回声,但是您也可以在数组中添加数组以仅返回数据部分,然后在javascript中进行处理。

For example (this would depend a bit on your exact data structure...): 例如(这将取决于您的确切数据结构...):

...
$data[] = array($m, $v['avg_handle_time']);
...

change show_aht.php to 将show_aht.php更改为

$res=array();
foreach($memo_data as $v){
        foreach($user_data as $m){
            if($v['memo_code'] == $m){
                $res[]= " User: " .$m. " Time: " . $v['avg_handle_time'];
            }
            elseif( $v['memo_code'] != $m){
                $res[]=  "User: " . $m . " Time: N/A <br>";
            }
        }
    }

echo json_encode($res);

and map.php ajax to 和map.php ajax到

$(document).ready(function() {
                $('#aht').click(function(){
                    $.ajax({
                    type:"POST",
                    url : "show_aht.php",
                    data: , // pass data here
                    dataType: 'json',
                    success : function(data){
                      for(i=0;i<data.length;i++){
                      //append data[i] to div
                      }
                    }//end success
                });//end ajax
              });//end click
            });//end rdy

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

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