I'm having problems creating nested object looping from Controller . This is the data that i'm currently using
What i've already tried is :
function getCoordinate() {
// Distict SQL Query for first loop
(SELECT DISTINCT(id_shipment) as id_shipment FROM tbl_tracking)
$id_shipment = $this->Maps_model->getIdShipment();
// First Loop
foreach ($id_shipment as $value) {
// The object i want to loop for views
$json = array(
'type' => 'Feature',
'properties' => json_decode('{}'),
'geometry' => array(
'type' => 'LineString',
'coordinates' => array()
));
//Query for getting coordinate
(SELECT a.latitude,a.latitude,b.ship_name from tbl_tracking a
LEFT JOIN tbl_shipment b on a.id_shipment = b.id
WHERE a.id_shipment = $value['id_shipment']);
$coordinate = $this->Maps_model->getCoordinate($value['id_shipment']);
//Second Loop inside The First Loop
foreach ($coordinate as $key) {
$json['geometry']['coordinates'] = array (
(float)$key['latitude'],
(float)$key['longitude']
);
}
echo json_encode($json);
}
}
The code provided above is not generating JSON Object . How do i generate the right loop for generating JSON Object ?
You did not provide much information, but try to do something like this.
function getCoordinate()
{
/* ... */
$coordinates = [];
// Second Loop inside The First Loop
foreach($coordinate as $key) {
/* ************* */
// note the "[]" to add new items
/* ************* */
$coordinates[] = array(
(float)$key['latitude'],
(float)$key['longitude']
);
}
$json['coordinates'] = $coordinates;
echo json_encode($json);
}
}