Richard Kyle
Richard Kyle

Reputation: 51

public variable cannot read by other function codeigniter

why my public variable cannot read by other function?

my public variable :

public $dataku = array();

first i set data to my public variable from model:

function cari() {

        $penerima = $this->input->post('penerima');
        $tanggal = $this->input->post('tanggal');
        $tanggal2 = $this->input->post('tanggal2');
        $id_komoditas = $this->input->post('id_komoditas');
        
        $this->dataku['judul'] = $this->judul;
        $this->dataku['pencarian'] = $this->mdl->get_data_antara_tanggal($penerima, $tanggal, $tanggal2, $id_komoditas);
        $this->dataku['url'] = $this->url;

        $title = "Data ".$this->judul;
        $subtitle = $this->url;
        $content = $this->load->view('cari.php', $this->dataku, true);

        $this->load_content($title, $subtitle, $content);
        
    }

but, when i want to read the public variable data again, it hasnt a data :

function check_data(){
        $data = isset($this->dataku['pencarian']) ? $this->dataku['pencarian'] : 'no data';
        print_r($data);
}

please, i need your help.

Upvotes: 0

Views: 38

Answers (1)

meewog
meewog

Reputation: 1740

you need to access the class properties with $this. Change the following lines:

$this->dataku['judul'] = $this->judul;
$this->dataku['pencarian'] = $this->mdl->get_data_antara_tanggal($penerima, $tanggal, $tanggal2, $id_komoditas);
$this->dataku['url'] = $this->url;

Also edit the following (you have an extra $):

function check_data(){
    $data = isset($this->dataku['pencarian']) ? $this->dataku['pencarian'] : 'no data';
    print_r($data);
}

Upvotes: 2

Related Questions