我正在使用 jQuery Upload File 插件并尝试将数据库集成到它。我需要像这样输出 json 响应的正确格式:
{"files": [
{
"id": 1,
"name": "picture1.jpg"
},
{
"id": 2,
"name": "picture2.jpg"
}
]}
我目前拥有的:
[
{
"id": 1,
"name": "picture1.jpg"
},
"id": 2,
"name": "picture2.jpg"
}
]
我的 php 文件如下所示:
$files= array();
$db = new DB;
$query = $db->get_rows("SELECT * FROM `files` ORDER BY `name`");
foreach ($query as $row) {
$file = new stdClass();
$file->id = $row->id;
$file->name = $row->name;
array_push($files,$file);
}
header('Content-type: application/json');
echo json_encode($files);