关于php:如何在codeigniter中显示检索数据

how display retrieve data in codeigniter

我的型号代码

1
2
3
4
5
6
7
8
9
public function retrivecat()
{
    $query = $this->db->get('category');
    foreach ($query->result() as $row)
    {
        $data[] = $row->catname;
    }
    return $data;
}

我的控制器代码

1
2
3
4
5
6
7
8
9
10
11
function addPost()
{
    $username = $this->session->userdata('username');
    $data = array(
        'pageTitle' => '?§?±?3?§ù? ù…?·ù??¨',
        'username' => $username
    );
    $this->load->model('category');
    $data = $this->category->retrivecat();
    $this->load->view('dashboard/post',$data);
}

我的查看代码:

1
2
print_r($data);
exit();

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

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: dashboard/post.php

Line Number: 72


要将变量传递到视图中,您必须这样做:

1
2
3
4
5
6
7
8
9
10
11
 function addPost()
{
    $username = $this->session->userdata('username');
    $data = array(
        'pageTitle' => '?§?±?3?§ù? ù…?·ù??¨',
        'username' => $username
    );
    $this->load->model('category');
    $data['variable'] = $this->category->retrivecat();
    $this->load->view('dashboard/post',$data);
}

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