我正在制作一个健身房列表,包括他们的名字,图片,团队,斑点,cp,所有者,id,lat,lon。
现在我知道它获得了每个查询等但它现在提供了许多相同的id,图像,团队等等,但它需要显示所有mon_cp mon_owner,mon_id因为每次mon_ *都是不同的。
它还向我显示错误:“SyntaxError:JSON.parse:在JSON数据的第1行第295行的JSON数据之后出现意外的非空白字符”
$sql = "SELECT
f.id, f.lat, f.lon, f.name, f.url, d.fort_id, d.pokemon_id, d.owner_name, d.cp, s.fort_id, s.team, s.slots_available
FROM forts AS f
LEFT JOIN gym_defenders AS d ON f.id=d.fort_id
LEFT JOIN fort_sightings AS s ON f.id=s.fort_id ORDER BY last_modified DESC";
$result = $mysqli->query($sql);
while($row = $result->fetch_assoc()) {
$url = preg_replace("/^http:/i", "https:", $row['url']);
if($row['team'] == 1){ $team = "mystic";}
if($row['team'] == 2){ $team = "valor";}
if($row['team'] == 3){ $team = "instinct";}
$encode = array("id" => $row['id'],
"name" => $row['name'],
"image" => $url,
"team" => $team,
"spots" => $row['slots_available'],
"mon_cp" => $row['cp'],
"mon_owner" => $row['owner_name'],
"mon_id" => $row['pokemon_id'],
"lat" => $row['lat'],
"lng" => $row['lon']);
echo json_encode($encode, JSON_FORCE_OBJECT);
}
需要输出以下内容:
name:Gymname
图片:https://images.com/image.png
团队:勇敢
mon_cp:1234,233
mon_owner:monowner2000,monowner232
mon_id:150,155
lat:34.67854
lon:5.054567
答案 0 :(得分:1)
你是结果集的每次迭代都是echo
- 一个完整的JSON对象。这将使回复无效。
简单地移动
echo json_encode($encode); // note, do NOT use JSON_FORCE_OBJECT
在while
循环之外,将$encode
分配更改为推送...
$encode[] = ['id' => ...
这将导致数组响应。