我不明白为什么这不能将数据库值放在页面上.您能告诉我这段代码在做什么吗?我是CodeIgniter的新手,这是我第一次尝试将数组从模型传递到控制器以进行查看.
控制器代码:
public function showuser(){
$id = $this->uri->segment(3);
$this->load->model('user_model');
$data['user']= $this->user_model->view_user($id);
$this->load->view('include/header');
$this->load->view('user_view',$data);
$this->load->view('include/footer');
}
型号代码:
public function view_user($id){
$this->db->where('userid', $id);
$query = $this->db->get('users');
return $query->result();
}
查看代码:
<table width="300" border="0" cellpadding="2">
<tr>
<td width="143">First Name</td>
<td width="143"><?php echo $data['user']->firstname; ?></td>
</tr>
<tr>
<td>Last Name</td>
<td><?php echo $data->lastname; ?></td>
</tr>
<tr>
<td>Company</td>
<td><?php echo $data->company; ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo $data->email; ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?php echo $data->phone; ?></td>
</tr>
<tr>
<td><p>Fax</p></td>
<td><?php echo $data->fax; ?></td>
</tr>
<tr>
<td>Address 1</td>
<td><?php echo $data->address1; ?></td>
</tr>
<tr>
<td><p>Address 2</p></td>
<td><?php echo $data->address2; ?></td>
</tr>
<tr>
<td>Address 3</td>
<td><?php echo $data->address3; ?></td>
</tr>
<tr>
<td>City</td>
<td><?php echo $data->city; ?></td>
</tr>
<tr>
<td>State/Region/Province</td>
<td><?php echo $data->state; ?></td>
</tr>
<tr>
<td>Postal Code</td>
<td><?php echo $data->zipcode; ?></td>
</tr>
<tr>
<td>Username</td>
<td><?php echo $data->username; ?></td>
</tr>
<tr>
<td>Account Type</td>
<td><?php echo $data->usertype; ?></td>
</tr>
</table>
解决方法:
您无需在视图内使用变量$data.只需通过$data数组键直接打印变量,例如$user-> firstname等.
有关视图的更多信息:http://ellislab.com/codeigniter/user-guide/general/views.html
另外,我认为您应该在模型内部返回$query-> row(),而不是$query-> result(),因为它仅获取一个用户的数据.