im与健身房一起列出一个名单,图像,团队,景点,CP,CP,所有者,ID,LAT,LON。
现在我有它可以获得每个查询等。但是现在它给出了许多相同的ID,图像,团队等。但是它需要显示所有mon_cp mon_owner,mon_id,因为每次mon_*都是不同的。
还向我展示了错误:" SyntaxError:JSON.PARSE:JSON DATA的JSON DATA的意外非Whitespace字符" JSON DATA的第2列列"
$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);
}
它需要输出以下内容:
名称:体操
图像:https://images.com/image.png
团队:Valor
mon_cp:1234,233
mon_owner:MONEWNER2000,MONEWNER232
mon_id:150,155
lat:34.67854
lon:5.054567
您是 echo
-ing一个完整的json对象,每个迭代的结果集。这将使响应无效。
只需移动
echo json_encode($encode); // note, do NOT use JSON_FORCE_OBJECT
在while
循环外面,然后将$encode
分配更改为推动...
$encode[] = ['id' => ...
这将导致数组响应。