-2

I want to echo the result from a query into a array.

These are the original lines:

{
$locations = array();
$locations[0] = array("image"=>"https://www.XX.nl/assets/img/prinsenhof.png","lat"=>"52.012136","lng"=>"4.354596","emp_name"=>"Museum Prinsenhof Delft","emp_id"=>"Museum Prinsenhof Delft");
$locations[1] = array("image"=>"https://www.XX.nl/assets/img/ampelman.png","lat"=>"51.8988595","lng"=>"4.4186571","emp_name"=>"Ampelmann Operations","emp_id"=>"Ampelmann Operations");
$locations[2] = array("image"=>"https://www.XX.nl/assets/img/koekamp.png","lat"=>"52.0821565","lng"=>"4.3202341","emp_name"=>"Koekamp The Hague","emp_id"=>"Koekamp The Hague");
$locations[3] = array("image"=>"","lat"=>"51.9179543","lng"=>"4.3986012","emp_name"=>"Grote of Sint Janskerk Schiedam","emp_id"=>"Grote of Sint Janskerk Schiedam");
$locations[4] = array("image"=>"","lat"=>"52.0596095","lng"=>"4.2219163","emp_name"=>"The International School of The Hague","emp_id"=>"The International School of The Hague");
$locations[5] = array("image"=>"","lat"=>"52.012663","lng"=>"4.3558941","emp_name"=>"Oude Kerk","emp_id"=>"Oude Kerk");
$locations[6] = array("image"=>"","lat"=>"52.0769897","lng"=>"4.3170919","emp_name"=>"Spuiplein","emp_id"=>"Spuiplein");
$locations = json_encode($locations);
}

So I put this info in a db and I want to display it This is what I made of it:

    $qry = "SELECT image, lat, lng, name, emp_id FROM googlemaps";
        if(!$result = $connection->query($qry)) {
            echo 'Fout in query: '.$mysqli->error;
        } else {
        $a = 0; 
        while ($location = $result->fetch_assoc()){

    $locations[$a] .= array("image"=>"https://www.XXXX.nl/assets/img/".$location['image'],"lat"=>$location['lat'],"lng"=>$location['lng'],"emp_name"=>$location['name'],"emp_id"=>$location['emp_id']);
    $a++;
            }
        }
        echo $locations;
echo $locations = json_encode($locations);

Any ideas for a better way?

errors:

Notice: Undefined offset: 0 in /index.php on line 137
Notice: Array to string conversion in /index.php on line 137
Notice: Array to string conversion in /index.php on line 137
Notice: Array to string conversion in /index.php on line 141
Coolen
  • 180
  • 2
  • 22
  • 1
    It's not clear to me what you're trying to accomplish here. I think you're trying to change a static array into one queried from a database? Could you show the output you expect? – Schwern Jul 22 '18 at 03:03
  • 1
    I hope I made the post more clear now. – Coolen Jul 22 '18 at 03:15

1 Answers1

1
Notice: Undefined offset: 0 in /index.php on line 137
Notice: Array to string conversion in /index.php on line 137
Notice: Array to string conversion in /index.php on line 137

This is because of:

$locations[$a] .= array("image"=>"https://www.XXXX.nl/assets/img/".$location['image'],"lat"=>$location['image'],"lng"=>$location['lng'],"emp_name"=>$location['name'],"emp_id"=>$location['emp_id']);

The first one is telling you that $locations[0] does not exist. The rest are because you're trying to do a string append to an array which doesn't make sense.

Notice: Array to string conversion in /index.php on line 141

This is because of echo $locations;. You can't just convert an array to a string. You'd need to use var_dump or print_r.


On to how to do this. First, turn on mysqli exceptions so you don't have to constantly check for error.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Then iterate through the rows pushing new results onto locations using locations[] = array(...).

$result = $connection->query("SELECT image, lat, lng, name, emp_id FROM googlemaps");
while ($location = $result->fetch_assoc()){
    $locations[] = array(
        "image" =>  "https://www.XXXX.nl/assets/img/".$location['image'],
        "lat"   =>  $location['lat'],
        "lng"   =>  $location['lng'],
        "emp_name"  =>  $location['name'],
        "emp_id"    =>  $location['emp_id']
    );
}

You can make this more efficient by doing it all in the query.

$result = $connection->query(<<<QUERY
    SELECT
        CONCAT("https://www.XXXX.nl/assets/img/", image),
        lat,
        lng,
        name as emp_name,
        emp_id
    FROM googlemaps
QUERY
);
while ($location = $result->fetch_assoc()){
    # now use $location directly
}

This avoids having to copy each row, and it avoids building up a potentially huge array of rows in memory.

If you do need to build up $locations, you can now do so in a single step with fetch_all, and it's a bit faster.

$locations = $result->fetch_all;
Schwern
  • 153,029
  • 25
  • 195
  • 336