I am trying to create a geoJSON array, but I am having difficulty access the values inside the query result. When I test my function by hardcoding the "coordinates" to the values '2' and '4' everything works fine.
However, when I then try to set the "coordinates" by referencing the $row['v2_lat']
and $row['v2_lng']
, this causes an error. Here is the PHP code:
function getGeoJSON2(){
$m = $this->input->POST('municipality');
$l = $this->input->POST('saleslimit');
$q = $this->db->query("SELECT RollNum, Address, v2_lat, v2_lng FROM tblontario WHERE Municipality = '".$m."'"." LIMIT ".$l);
$res = $q->result();
if(!empty($res)){
$geoArr = Array(
"type" => "MultiPoint",
"coordinates" => Array()
);
foreach ($q->result() as $row) {
//$geoArr["coordinates"][] = Array(2,4);
$geoArr["coordinates"][] = Array($row['v2_lat'],$row['v2_lng']);
}
$geoJSON = json_encode($geoArr);
echo $geoJSON;
} else {
echo "{}";
}
}
And here is a sample of the query result and after applying json_encode
:
[
{
"RollNum": "193601001000100",
"Address": "12 STEELES AVE E",
"v2_lat": "43.8561002",
"v2_lng": "-79.3370188"
},
{
"RollNum": "193601001000400",
"Address": "18 STEELES AVE E",
"v2_lat": "43.7986849",
"v2_lng": "-79.4178564"
},
{
"RollNum": "193601001002100",
"Address": "36 STEELES AVE E",
"v2_lat": "43.7987492",
"v2_lng": "-79.4169781"
}
]
I don't understand why I can't seem to be able to access the $row['v2_lat']
and $row['v2_lng']
values in my foreach
loop. Any ideas? Thanks.
EDIT: I wanted to see the $res
variable and this is the format of the array that comes back from the query:
Array
(
[0] => stdClass Object
(
[RollNum] => 180501000100100
[Address] => 2 HILEY AVE
[v2_lat] => 43.8526509
[v2_lng] => -79.0499877
)
[1] => stdClass Object
(
[RollNum] => 180501000100200
[Address] => 4 HILEY AVE
[v2_lat] => 43.8526678
[v2_lng] => -79.0501957
)
[2] => stdClass Object
(
[RollNum] => 180501000100300
[Address] => 6 HILEY AVE
[v2_lat] => 43.8526848
[v2_lng] => -79.0504037
)
)
What is the significance of the difference between these last two results?