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); } |
我的查看代码:
但是,这不起作用并显示:
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); } |
然后在视图中做
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/php/268439.html