所以看起来我需要更多的帮助,我正在尝试从我的数据库中获取记录,以便在我的视图页面上显示每行下降的行,但我只是得到HTTP 500错误,不知道我在哪里出了问题。
ci_users - 数据库架构
id(PK, int, not null)
user_name(nchar255, not null)
user_email(nchar255, not null)
user_password(nchar255, not null)
user_displayname(nchar255, not null)
user_active(smallint, not null)
user_level(smallint, not null)
userlist_view
<table cellSpacing="0" cellPadding="4" width="100%" border="0">
<tr bgColor="#a5a6a9">
<td width="20%" align="left"><b>User ID</b></td>
<td width="20%" align="left"><b>Username</b></td>
<td width="20%" align="center"><b>Email Address</b></td>
<td width="20%" align="center"><b>Displayname</b></td>
<td width="20%" align="center"><b>User Level</b></td>
</tr>
<?php
foreach ($result as $result) ?>
<tr>
<td width="20%" align="left"><?php echo $result[0]->id; ?></td>
<td width="20%" align="left"><?php echo $result[0]->user_name; ?></td>
<td width="20%" align="left"><?php echo $result[0]->user_email; ?></td>
<td width="20%" align="left"><?php echo $result[0]->user_displayname; ?></td>
<td width="20%" align="left"><?php echo $result[0]->user_level; ?></td>
<?php endforeach; ?>
</table>
用户列表控制器
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class userlist extends CI_Controller {
public function __construct(){
parent::__construct();
}
function index()
{
$this->load->view('userlist_view');
}
function records()
{
$data = array();
$this->load->model('userlist_database');
$result = $this->userlist_database->getUsers();
$this->load->view('userlist_view', $result);
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
}
?>
userlist_database模型
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class userlist_database extends CI_Model{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->database();
}
function getUsers() {
$this->db->select('id','user_name','user_email','user_displayname','user_level');
$this->db->from('ci_users');
$query = $this->db->get();
return $result = $query->result();
}
}
?>
答案 0 :(得分:1)
在
</tr>
循环中遗失foreach
并改进了
在模型中
function getUsers() {
$this->db->select('id','user_name','user_email','user_displayname','user_level');
$this->db->from('ci_users');
$query = $this->db->get();
return $result = $query->result_array();
}
在控制器中
$data['result'] = $this->userlist_database->getUsers();
$this->load->view('userlist_view', $data);
在视图中
<?php
foreach ($result as $item)
{
?>
<tr>
<td width="20%" align="left"><?php echo $item['id']; ?></td>
<td width="20%" align="left"><?php echo $item['user_name']; ?></td>
<td width="20%" align="left"><?php echo $item['user_email']; ?></td>
<td width="20%" align="left"><?php echo $item['user_displayname']; ?></td>
<td width="20%" align="left"><?php echo $item['user_level']; ?></td>
</tr>
<?php } ?>