1

Good day, i'm trying to save to my table.

function save()
{
    extract(populateform());
    echo "<pre>";print_r(populateform());
    $tipenya = $this->modelmodel->showdata("SELECT * From user_type");
    foreach($tipenya as $types)
    {
        if($pilihan[$types->id_tipe] != ''){    
        foreach($pilihan[$types->id_tipe] as $values){
                echo "insert into tbl a value ('".$values.','.$types->id_tipe."')<br>";
            }
        }
    }
}

and here is data that i want to save

Array
(
    [pilihan] => Array
        (
            [TP001] => Array
                (
                    [0] => 2
                )

            [TP003] => Array
                (
                    [0] => 2
                )

        )

)

the result from print_r($tipenya)

Array
(
    [0] => stdClass Object
        (
            [id_tipe] => TP001
            [deskripsi] => Developer
            [flag] => 1
        )

    [1] => stdClass Object
        (
            [id_tipe] => TP002
            [deskripsi] => Admin
            [flag] => 1
        )

    [2] => stdClass Object
        (
            [id_tipe] => TP003
            [deskripsi] => Outlet
            [flag] => 1
        )

)

and here is the error Message: Undefined index: TP002. But when i try to input this

Array
(
    [pilihan] => Array
        (
            [TP001] => Array
                (
                    [0] => 1
                )

            [TP002] => Array
                (
                    [0] => 1
                )

            [TP003] => Array
                (
                    [0] => 1
                )

        )

)

there aren't any error. I'm trying use this but no help

if($pilihan[$types->id_tipe] != ''){  }
YVS1102
  • 2,658
  • 5
  • 34
  • 63
  • change `if($pilihan[$types->id_tipe] != '')` to `if(isset($pilihan[$types->id_tipe]) && $pilihan[$types->id_tipe] != '')` – Chetan Ameta Jun 21 '16 at 09:21
  • Perhaps you want `$pilihan['pilihan'][$types->id_tipe]`...? It's a bit unclear what's what here. – deceze Jun 21 '16 at 09:29

2 Answers2

0

Use isset():

if (isset($pilihan[$types->id_tipe])) {
Shira
  • 6,392
  • 2
  • 25
  • 27
0

Use isset() to check if your index is set or not .

Isset just checks if is it set, it could be anything not null

if(isset($pilihan[$types->id_tipe]) && $pilihan[$types->id_tipe] != ''){ 

OR

Empty checks if the variable is set and if it is it checks it for null, "", 0, etc

if(!empty($pilihan[$types->id_tipe]))
Brijal Savaliya
  • 1,101
  • 9
  • 19