【发布时间】:2026-01-10 20:15:02
【问题描述】:
我想要做的是使用 PHP foreach 将所有用户和与他们相关的数据的列表附加到表行,该信息是从数据库中获取的。我遇到的问题是以下错误:
[严重性:错误消息:调用数组上的成员函数 result_array()]
我阅读了similar question 关于此的内容,但它似乎无法解决我的问题,因为当我从模型或控制器。
这是 User.php 模型:
Class User extends CI_Model
{
function populateUsers()
{
$this->db->select('username, accessLevel, fullName');
$this->db->from('users');
$userquery = $this->db->get();
if($userquery -> num_rows()>=1)
{
return $userquery->result_array();
}
else
{
return false;
}
}
}
这是来自 AdminCP.php 控制器的 sn-p:
public function index()
{
$this->load->helper(array('form')); // for user creation
$session_data = $this->session->userdata('logged_in');
$aLevel = $session_data['accessLevel'];
if($this->session->userdata('logged_in') && $aLevel == 'Admin')
{
$userResult = $this->user->populateUsers();
if($userResult)
{
$user_array = array();
foreach($userResult as $row)
{
$user_array = array(
'username' => $row->username,
'accessLevel' => $row->accessLevel,
'fullName' => $row->fullName
);
$data['accessLevel'] = $session_data['accessLevel'];
$data['username'] = $session_data['username'];
$data['fullName'] = $session_data['fullName'];
$data['userList'] = $user_array;
$this->load->view('admin_cp_view', $data);
}
return TRUE;
}
else
{
return FALSE;
}
}
else
{
redirect('home', 'refresh');
session_write_close();
}
}
最后,这是 Admin_CP_View.php 页面的部分,我想在其上显示此信息:
<div class="well">
<h4>Manage users</h4>
<p>Select a user from the list to manage</p>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Access Level</th>
<th>Full Name</th>
</tr>
</thead>
<tbody>
<?php foreach ($userList->result_array() as $row)
{ ?>
<tr>
<td><?php echo $row['username'];?></td>
<td><?php echo $row['accessLevel'];?></td>
<td><?php echo $row['fullName'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
请告诉我这里哪里出错了,谢谢!
【问题讨论】:
标签: php mysql arrays codeigniter