从PHP查询中获取JSON数组后,用javascript编写JSON数组到html页面
Writing JSON array to html page with javascript after getting it from PHP query
我有一个MySQL查询,我传递给一个PHP数组。PHP数组使用json_encode
转换为JSON数组。我试图然后得到这个打印到我的html页面。以下是我到目前为止写的。
<?php
class db {
public $conn;
function dbLogin() {
$this->conn = new mysqli("1.2.3.4","user","pwd","db");
}
public function selectQuery() {
$this->dbLogin();
$query = "
SELECT *
FROM tekken_7_frame_data
";
echo "<pre>";
$resultArray = Array();
if ($result = $this->conn->query($query)) {
while ($row = $result->fetch_array()) {
$resultArray[] = $row;
}
}
echo "</pre>";
$resultJson = json_encode($resultArray);
}
}
$fData = new db();
$fData->selectQuery();
?>
<html>
<head>
</head>
<body>
<script>
var jsonArray = <?php echo $resultJson ?>;
document.write(jsonArray);
for (i = 0; i < jsonArray.length; i++) {
document.write(jsonArray[i][1]);
}
</script>
</body>
</html>
我已经在StackOverflow上阅读了其他类似的问题,没有运气;我的HTML页面完全是空白的。我试过把json_econde(...)
在javascript变量jsonArray
内,而不是在我的PHP类。这也没起作用。我还var_dump
'd PHP数组没有问题(整个事情显示在页面上),以及使用json_encode
将数组放在PHP变量中,然后响应该数组没有问题。
我不知道如何访问这个JSON数组。我最终希望它在一个表中,但因为这是我第一次弥合PHP和Javascript之间的差距,我想我应该一步一步来,这样我就能确切地知道发生了什么。
尝试改变你的函数(不要使用echo
!but return
something) to:
public function selectQuery() {
$this->dbLogin();
$query = "
SELECT *
FROM tekken_7_frame_data
";
$resultArray = Array();
if ($result = $this->conn->query($query)) {
while ($row = $result->fetch_array()) {
$resultArray[] = $row;
}
}
return json_encode($resultArray);
}
和下体:
$fData = new db();
$resultJson = $fData->selectQuery();
从PHP获取数据到JavaScript的最佳方法是使用异步请求(即"AJAX")。
javascript库极大地简化了这个过程。下面是一个使用AngularJS的粗略示例(http://angularjs.org)。
json.php
// this file JUST needs to print JSON as string.
<?php
class db {
public $conn;
function dbLogin() {
$this->conn = new mysqli("1.2.3.4","user","pwd","db");
}
public function selectQuery() {
$this->dbLogin();
$query = "
SELECT *
FROM tekken_7_frame_data
";
$resultArray = Array();
if ($result = $this->conn->query($query)) {
while ($row = $result->fetch_array()) {
$resultArray[] = $row;
}
}
return json_encode($resultArray);
}
}
$fData = new db();
print $fData->selectQuery();
?>
myscript.js
// this will fetch your JSON string from the PHP file and
// set $scope.myData to a JS object.
// it sets $scope.myDataString to a JSON stringified format.
angular.module('myApp' [])
.controller('myController', function($http, $scope) {
var $scope.myData = {};
var $scope.myDataString = '';
function getMyData () {
$http.get('/json.php')
.success( function (data) {
$scope.myData = data;
$scope.myDataString = JSON.stringify(data);
})
.error(function (err) {
$scope.myData = {error: err};
$scope.myDataString = "ERROR: "+JSON.stringify(data);
})
}
getMyData();
}
mypage.html
<!-- include angular and your app's file.
make sure to link in your app and controller -->
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src='myscript.js'>
</head>
<body ng-app='myApp'>
<main ng-controller='myController as myCtrl'>
<div id='myOutput'>{{myDataString}}</div>
</main>
</body>
</html>
相关文章:
- 需要帮助设置json数组
- 如何使用 node.js 比较两个 json 数组
- 根据id将json数组组合为一个json数组
- 如何创建JSON数组
- 如何将package.json数组传递给grunt.js
- 访问json数组中的对象
- JSON数组数据返回Undefined
- 如何向JSON数组动态添加属性
- 如何从json数组中获取特定值
- 从多维嵌套json数组创建下拉列表
- 在ListView中添加JSON数组中存储的图像-ReactNative
- jquery在json数组中循环
- 将 JSON 数组解析为 JavaScript 数组
- 如何在php中读取没有任何键的JSON数组
- 如何在Jquery中迭代JSON数组
- 如何在cycle js中从JSON数组创建组件
- 如何使用jquery返回php-json数组对象
- JSON数组转换为JS对象数组
- 使用System从C#集合创建JSON数组.网状物剧本序列化
- 使用Underscore.js修改json数组中所选元素的更有效方法