1
votes

my model code

public function retrivecat()
{
    $query = $this->db->get('category');
    foreach ($query->result() as $row)
    {
        $data[] = $row->catname;
    }
    return $data;
}

My controller code

function addPost()
{
    $username = $this->session->userdata('username');
    $data = array(
        'pageTitle' => 'ارسال مطلب',
        'username' => $username
    );
    $this->load->model('category');
    $data = $this->category->retrivecat();
    $this->load->view('dashboard/post',$data);
}

my view code :

print_r($data);
exit();

but, this is not worked and display :

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: dashboard/post.php

Line Number: 72

1

1 Answers

0
votes

For passing variables into the view you must do this:

 function addPost()
{
    $username = $this->session->userdata('username');
    $data = array(
        'pageTitle' => 'ارسال مطلب',
        'username' => $username
    );
    $this->load->model('category');
    $data['variable'] = $this->category->retrivecat();
    $this->load->view('dashboard/post',$data);
}

Then in the view do print_r($variable);