Andre Bhaskoro Andre Bhaskoro - 2 months ago 9
PHP Question

Codeigniter - how to passing foreach value to inside of array

I use CodeIgniter 3 and libraries of http://biostall.com/codeigniter-google-maps-v3-api-library/ .
When I take the data from the database and put it into the polygon points, I encountered some error .


Severity: Warning Message: trim() expects parameter 1 to be string,
array given Filename: libraries/Googlemaps.php
Line Number: 2179


my controller function :

public function testing()
{
$config['center'] = '-8.1768084, 113.6559507';
$config['zoom'] = 'auto';
$this->googlemaps->initialize($config);

$kecamatan = $this->Peta_model->get_kecamatan();
foreach ($kecamatan as $k) {
$kcmt = $k->name;
$coor = $this->Peta_model->get_coor_by_kcmt($kcmt);
if ($coor) {
$polygon = array(
'points' => array() ,
'strokeColor' => '#000099' ,
'fillColor' => '#000099' ,
);
foreach ($coor as $c) {
$polygon ['points'][] = array("'".$c->latitude.",".$c->longitude."'");
}
$this->googlemaps->add_polygon($polygon);
}
}


$user = $this->ion_auth->user()->row();
$user_level = $this->ion_auth->get_users_groups($user->id)->row();
$data = array(
'user' => $user ,
'user_level' => $user_level ,
'title' => 'Peta' ,
'content' => 'peta/v_peta' ,
'map' => $this->googlemaps->create_map(),
);

$this->load->view('dashboard/layout', $data);
}


normaly add polygon is :

$polygon = array(
'points' => array('37.425, -122.1321',
'37.4422, -122.1622',
'37.4412, -122.1322',
'37.425, -122.1021') ,
'strokeColor' => '#000099' ,
'fillColor' => '#000099' ,
);
$this->googlemaps->add_polygon($polygon);


how do I fix it ?

Answer

You are storing an array in $polygon ['points'][].

i think you should store a string. like..

$polygon ['points'][] = "'".$c->latitude.",".$c->longitude."'";