我目前迷失了如何做到这一点并需要帮助但是我正在构建一个离子应用程序并在通过PHP脚本获取该信息时在数据库中存储信息我正在收到JSON我想要转换为一个很好的格式化HTML表
这是我的PHP
>>> import pprint
>>> import pymongo
>>> client = pymongo.MongoClient()
>>> collection = client['test']['collection']
>>> pipeline = [{'$project': {'category': 1,
... 'name': 1,
... 'w': {'$cond': [{'$eq': ['$category', 'B']},
... 1,
... {'$cond': [{'$eq': ['$category', 'C']},
... 2,
... 3]
... }]
... }
... }},
... {'$sort': {'w': 1}}
... ]
>>> pprint.pprint(list(collection.aggregate(pipeline=pipeline)))
[{'_id': ObjectId('571caa930e4f55302502a361'),
'category': 'B',
'name': '456',
'w': 1},
{'_id': ObjectId('571caa930e4f55302502a363'),
'category': 'C',
'name': '101',
'w': 2},
{'_id': ObjectId('571caa930e4f55302502a360'),
'category': 'A',
'name': '123',
'w': 3},
{'_id': ObjectId('571caa930e4f55302502a362'),
'category': 'A',
'name': '789',
'w': 3}]
这是Json的输出:
<?php
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Origin: *");
$host = "****";
$user = "*****";
$password = "";
$database = "holes";
$connect = mysqli_connect($host,$user,$password,$database) or die("Problem connecting.");
$result = mysqli_query($connect, "SELECT * from hole18") or die("Bad Query.");
mysqli_close($connect);
$results = array();
while($row = mysqli_fetch_assoc($result))
{
$results[] = $row;
}
echo json_encode($results);
?>
请帮帮我
答案 0 :(得分:2)
试试这个
<table>
<?php
$json_result = json_encode($results);
$json_dec_result = json_decode( $json_result );
if ( !empty( $json_dec_result) )
{
foreach ($json_dec_result as $field_name => $field_value)
{
?>
<tr>
<td>
<?php echo $field_name;?>
</td>
<td>
<?php echo $field_value;?>
</td>
</tr>
<?php
}
}
?>
</table>
答案 1 :(得分:1)
Uttam的答案将用于从您的PHP代码获取HTML表服务器响应。这可能就是你所需要的,但是如果你使用的是Ionic,那么更合适的方法可能仍然是坚持使用JSON响应,将其加载到控制器中并使用ng-repeat和数据网格将其显示为HTML。
如果您有兴趣,请查看此内容:creating a table in ionic