[英]convert data from mysql db to table using an json/php api
I'm trying to make my own api using a JSON and PHP script, which is successfully working (api.mystem.tk/product), however I want to convert that data to an html table with the columns: id, name and date. 我正在尝试使用JSON和PHP脚本制作自己的api,该脚本可以成功运行(api.mystem.tk/product),但是我想将该数据转换为带有以下列的html表:id,名称和日期。
My question, how? 我的问题,如何? I've got my code included and you can watch the JSON output in the console of api.mystem.tk/product. 我已经包含了我的代码,您可以在api.mystem.tk/product的控制台中观看JSON输出。 I've deleted all the private details in the scripts and replaced them with #______. 我已删除了脚本中的所有私人详细信息,并将其替换为#______。
api.php : api.php :
<?php
$connect = mysqli_connect('#host', '#user', '#password', '#db');
if(!isset($_GET['function'])) {
die('Some error occurred!');
}
function GetProducts($db) {
$sql = mysqli_query($db, 'SELECT * FROM php_test ORDER BY Id ASC LIMIT
0, 10');
$data = array();
if (mysqli_num_rows($sql) > 0) {
while ($row = mysqli_fetch_array($sql)) {
$data[] = $row["Id"];
$data[] = $row["Name"];
$data[] = $row["Date"];
}
}
$data = json_encode($data);
echo $_GET['jsonCallback'].'('.$data.')';
}
if (function_exists($_GET['function'])) {
$_GET['function']($connect);
}
echo $data;
?>
function .js: 函数 .js:
$(function(){
var functionName = 'GetProducts';
function LoadData() {
$.getJSON("http://api.mystem.tk/product/api.php?
function="+functionName+"&jsonCallback=?", function(data) {
var all_data = [];
$.each(data, function(k, name){
var array_data = '<div class="names">'+name+'</div>';
all_data.push(array_data);
});
$('#data').append(all_data);
console.log(data);
});
}
LoadData();
});
index .php: 索引 .php:
<!DOCTYPE html>
<html lang="en">
<head>
<title> API TEST </title>
<meta name="viewport" content="width=device-width,initial-
scale=1,maximum-scale=1,user-scalable=0">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
</head>
<body>
<div id="data"></div>
<script type="text/javascript" src="function.js"></script>
</body>
</html>
You'll need to parse JSON data in your function.js using jQuery.parseJSON() jQuery function. 您需要使用jQuery.parseJSON() jQuery函数在function.js中解析JSON数据。 You can read brief documentation from: jQuery.parseJSON() 您可以从以下网址阅读简要文档: jQuery.parseJSON()
Added code: 添加的代码:
<script>
var obj = JSON.stringify([
{
"id":'1',
"name":'Jhon Doe',
"date":'April 14,2018'
},
{
"id":'2',
"name":'Coder',
"date":'April 23, 2018'
}
]);
var jsontoparse = JSON.parse(obj);
var i = obj.length;
var j=0;
while(j<=i){
console.log(jsontoparse[j].id);
j = j+1;
}
</script>
so i've tried using $.parseJSON instead of $getJSON, but without succes console: 所以我尝试使用$ .parseJSON代替$ getJSON,但是没有成功的控制台:
Uncaught SyntaxError: Unexpected token <
at m (jquery-3.3.1.min.js:2)
at Function.globalEval (jquery-3.3.1.min.js:2)
at text script (jquery-3.3.1.min.js:2)
at Ut (jquery-3.3.1.min.js:2)
at k (jquery-3.3.1.min.js:2)
at XMLHttpRequest.<anonymous> (jquery-3.3.1.min.js:2)
and i've changed this: 我已经改变了这个:
$data[] = $row["Id"];
$data[] = $row["Name"];
$data[] = $row["Date"];
to this: 对此:
$data[] = array(
'id' => $row["Id"],
'name'=> $row["Name"],
'date'=> $row["Date"]
)
to be a little bit more specified... 更具体点...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.