首页 > 解决方案 > 如何在 HTML 表格中显示 JSON 数据

问题描述

wp_posts我有一个 WordPress 表,我必须从ID 为 30 的位置获取数据。

我想,让我们先检查一下 id 30 是否可用。我发现 id 30 和数据显示如下结构

ID     | post_title    | post_content
30     | testing       | [["Name","Industry","Number","Website"],["pqr","Other","10","pqr.com"],["lmn","Other","15","lmn.com"],["xyz","Other","20","xyz.com"]]

现在我尝试了下面的代码来获取数据

进程.php

这是我的动作名称 emplist 代码

$query="SELECT * FROM `wp_posts` where ID=30";

try {
          $stmt = $pdo->prepare($query);
          $stmt->execute();
          $row = $stmt->fetch();

        $arr_result= array(
                    "id" =>$row['ID'],
                    "postcontent" => $row['post_content']
                     );
    
        }
        catch(PDOException $e) {
            echo "Error: " . $e->getMessage();
        }

        print_r($arr_result);

        //echo json_encode($arr_result);

现在我得到以下输出

Array ( 
       [id] => 30 
       [postcontent] => [
                          ["Name","Industry","Number","Website"],
                          ["pqr","Other","10","pqr.com"],
                          ["lmn","Other","15","lmn.com"],
                          ["xyz","Other","20","xyz.com"],
                        ] 

            ) 

I have the below script when the page loads then it will call the process.php

<script type="text/javascript">
        $(document).ready(function(){
            $.ajax({
                url: 'process.php',
                method:'post',
                dataType: "json", 
                data:{action:"emplist"},  
                success: function(data){
                var htmlText = '';
                for (var key in data['postcontent']) {

                htmlText += '<div>' + data[key].postcontent + '</div>'; 
                }
                $('body').append(htmlText);
                    
                }
            });

        });
    </script>

我的问题是如何在 html 表中显示上述输出?

我的 console.log(data) 输出是

Object {
             id: 30, 
             postcontent: "[[\"Name\",\"Industry\",\"Number\",\"Website\"],
                           [\"pqr\",\"Other\",\"10\",\"pqr.com\"],
                           [\"lmn\",\"Other\",\"15\",\"lmn.com\"],
                           [\"xyz\",\"Other\",\"20\",\"xyz.com\"],

                           ]" }

标签: phphtmljquerywordpresstablepress

解决方案


推荐阅读