0

Im making a list with gyms, with their names,image, team, spots, cp, owner, id, lat, lon.

Now i have it that it gets every query etc. but it gives now many same ids, images, team, etc. but it needs to show all mon_cp mon_owner, mon_id because those mon_* are all different each time.

also it show me the error: "SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 295 of the JSON data"

    $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);
}

it needs to output the following:

name: Gymname

image: https://images.com/image.png

team: valor

mon_cp: 1234, 233

mon_owner: monowner2000,monowner232

mon_id: 150, 155

lat: 34.67854

lon: 5.054567

2
  • You are echo-ing a complete JSON object each iteration of your result-set. This will make the response invalid Commented Sep 8, 2017 at 1:49
  • how should i handle it then because if i echo it below the } of the while loop it only shows 1 item. @Phil Commented Sep 8, 2017 at 1:51

1 Answer 1

1

You are echo-ing a complete JSON object each iteration of your result-set. This will make the response invalid.

Simply move

echo json_encode($encode); // note, do NOT use JSON_FORCE_OBJECT

outside the while loop and change the $encode assignment to a push...

$encode[] = ['id' => ...

This will result in an array response.

Sign up to request clarification or add additional context in comments.

6 Comments

That makes my errors go indeed away but it shows me now so many items look how my example output needs to be. It gives now hundreds of items with the same info. they only need other values to it when they have more so you have a list in one. like mon_owner: monowner2000, monowner3
@PrizenTV sorry, you're not making much sense. If your query returns too many results, then you should change it to only return the data you want
at my point it only needs to show 7 items because i have only 7 gyms, but it gives me 9999 items.
imgur.com/a/VyaIG you see same id's only some of them have different mon_* values, those needs to be in 1. in the image you see many id: 5 and 1 id: 9.
@PrizenTV seems your database has duplicate values then. That and/ or your gym_defenders and / or fort_sightings tables are many-to-one with forts in which case, getting repeated forts values is expected when using joins
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.