如何在codeigniter中显示检索数据

时间:2015-11-23 08:37:45

标签: php codeigniter

我的型号代码

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

我的控制器代码

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);
}

我的观看代码:

print_r($data);
exit();

但是,这不起作用并显示:

  

遇到PHP错误

     

严重性:注意

     

消息:未定义的变量:数据

     

文件名:dashboard / post.php

     

行号:72

1 个答案:

答案 0 :(得分:0)

要将变量传递到视图中,您必须执行以下操作:

 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);
}

然后在视图中执行print_r($variable);