Reputation: 2277
How can fix error Message: Invalid argument supplied for foreach() - Line Number: 28
in following foreach
?
<?php
$mileage = array();
$mileage_input = $this->input->post('mileage');
foreach ($mileage_input as $idx => $name) { //Line 28
$mileage[] = array(
'mileage' => $mileage_input[$idx]
);
}
$data = array(
'mileage' => json_encode($mileage),
'customer_number' => $customer_number,
'name' => $this->input->post('name')
);
$this->db->insert('customer', $data);
?>
Upvotes: 1
Views: 27284
Reputation: 1928
Most likely $mileage_input is not an array. Perhaps you must error check $this->input->post.
If you dont really care about the error, but simply want to not get the error you can cast the value to an array before looping over it.
foreach((array)$mileage_input as $idx => $name {
Upvotes: 1
Reputation: 3981
$mileage_input
is probably not an array, which is why it isn't working.
Upvotes: 7