php - 如何在 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\"],
]" }
解决方案
推荐阅读
- php - Wordpress Avada 主题 - 删除 Woocommerce 结帐字段的 Square 付款问题
- javascript - 未捕获的类型错误:传输事件时无法读取未定义的属性“clientX”
- python - chromedriver 找不到带有 robotsframework 的 google chrome 二进制文件
- algorithm - 具有开放寻址、非延迟删除(无墓碑)的哈希表
- json - 将 Excel 中的参数用引号括起来
- xaml - 相对于屏幕缩放 UI
- android - 由于致命错误,我无法实现 CursorLoader
- vhdl - 如何使用 VHDL/Block 设计将 BRAM 重置为初始内容
- string - 如何为 wc --files0-from=F 指定以 NUL 结尾的文件名称
- matplotlib - Matplotlib 颜色条缩放