Muhammad Aldiansyah
Muhammad Aldiansyah

Reputation: 3

Invalid argument supplied for foreach() Line 140

Error ON 140 , invalid argument supplied for foreach() , i dont know what i have to do

MyController code :

if ($data['ms']['mspr_no'] != '') {
    $mdpr = $this->input->post('mdpr');
    foreach ($mdpr as $key => $value1) {
        foreach ($mdpr[$key] as $idx => $value2) {
            if ($mdpr['mdpr_mspd_no'][$idx] != '') {
                $data['md'][$idx]['mdpr_no'] = $data['ms']['mspr_no'];

                if ($key == 'mdpr_net') {
                    $data['md'][$idx][$key] = ftodouble($value2);
                } else {
                    $data['md'][$idx][$key] = $value2;
                }
            }
        }
    }
}

My Form Promo view :

    <?php
    if ($action == 'view') {
        if (isset($promo)) {
            foreach ($promo as $value) { ?>
                <tr>                                                      
                    <td><input type="text" name="mdpr[mdpr_mspd_no][]" class="form-control srstyle-ta-center mdpr_mspd_no" value="<?php echo $value['mdpr_mspd_no']; ?>" readonly /></td>
                    <td><input type="text" class="form-control msph_pl" value="<?php echo $value['msph_pl']; ?>" readonly/></td>
                    <td><input type="text" class="form-control msph_hjkk_net" value="<?php echo $value['msph_hjkk_net']; ?>" readonly/></td>
                    <td><input type="text" class="form-control msph_hjkj_net" value="<?php echo $value['msph_hjkj_net']; ?>" readonly/></td>
                    <td><input type="text" name="mdpr[mdpr_net][]" class="form-control mdpr_net srstyle-ta-right auto-numeric" value="<?php echo $value['mdpr_net']; ?>" readonly/></td>
                </tr><?php
            }
        }

    }
    ?>  

Thanks

Upvotes: 0

Views: 85

Answers (2)

Bira
Bira

Reputation: 5506

Use a if(is_array()) to validate that is a array to be looping.

Upvotes: 0

Akshay Hegde
Akshay Hegde

Reputation: 16997

“Warning: Invalid argument supplied for foreach() ”

Its because of either null or false or argument is not array

foreach($mdpr as $key => $value1){ 
             ^
            see var_dump($mdpr), 
            $mdpr is either null or false or not array you get above error

So what you can do is

$mdpr = $this->input->post('mdpr');
if(is_array($mdpr))
{
    foreach($mdpr as $key => $value1)
    { 
            // your remaining code goes here
    }
}else
{
         echo 'Bad data posted';
}

We can regenerate this error like this for example

$ php -r 'foreach(null as $e){}'
PHP Warning:  Invalid argument supplied for foreach() in Command line code on line 1

/*false OR FALSE*/
$ php -r 'foreach(false as $e){}'
PHP Warning:  Invalid argument supplied for foreach() in Command line code on line 1

/* Not array */
$ php -r 'foreach("somestring" as $e){}'
PHP Warning:  Invalid argument supplied for foreach() in Command line code on line 1

I see OP made an edit - Line No : 140 enter image description here

Upvotes: 1

Related Questions