Jennifer Anthony
Jennifer Anthony

Reputation: 2277

Invalid argument supplied for foreach()

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

Answers (2)

Alexander Olsson
Alexander Olsson

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

Chris G.
Chris G.

Reputation: 3981

$mileage_input is probably not an array, which is why it isn't working.

Upvotes: 7

Related Questions