CodeIgniter - 变量未定义

I put code like this in application/views/pengaduan_insert.php:

<?php echo form_dropdown('kategori',$kategori,'', $js); ?>

I have tried to define $kategori in controller and model, but it always show the same error,

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: kategori

Filename: admin/pengaduan_insert.php

Line Number: 48

I have declared $kategori $kategori = $this->usermodel->ambil_kategori();

            //ekspand data
    foreach($kategori as $d) {
                $data['kategori'][0] = "-Pilih Kategori-";
                $data['kategori'][$d->id_kategori] = $d->kategori;
            }

Can anyone help me? Thanks a lot.

if the variable is undefined, then define it.

$kategori = array(
    'option 1',
    'option 2',
    'option 3',
);

you have to pass your variable to view as well, like from function in controller, do:

$data["kategory"] = $kategory;
..
$this->load->view('some_view', $data);

See:: Reference Without looping the data in your controller function, just do:

$kategori = $this->usermodel->ambil_kategori();
$data["kategory"] = $kategory;
...
$this->load->view('some_view', $data);

and in your view check if your variable is not empty:

if(!empty($kategory)) {
   echo form_dropdown('kategori',$kategori,'', $js);
}

Do you really loop inside the foreach ? If not, $data['kategory'] will never exist. You should put the $data['kategori'][0] = "-Pilih Kategori-"; before the foreach