我想做的是使用PHP foreach将所有用户的列表和与他们相关的数据附加到表行中,其中此信息是从数据库中获取的。我遇到的问题是以下错误:
[严重性:错误消息:调用数组上的成员函数 result_array() ]
我读过一个类似的问题,但它似乎并不能解决我的问题,因为从模型或控制器中删除 result_array() 部分时,我会遇到许多与尝试从非对象获取属性有关的错误。
下面是用户.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 控制器的片段:
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>
请让我知道我在这里出错的地方,谢谢!
在 AdminCP 中编辑
.phppublic 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)
{
$this->load->view('admin_cp_view', $userResult);
}
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 ($userResult 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>
这可能会对你有所帮助..谢谢!
你有几个问题。首先,与其说是一个问题,不如说它是毫无价值的代码。控制器不需要返回值,因为 Codeigniter 不需要一个值,并且无论如何都不会关注它。
其次,我已从index
中删除了这些行,因为它们在视图中似乎未使用。
$data['accessLevel'] = $session_data['accessLevel'];
$data['username'] = $session_data['username'];
$data['fullName'] = $session_data['fullName'];
下面是控制器的修订索引函数,删除了返回 TRUE 或 FALSE 的逻辑。
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')
{
$data['userList'] = $this->user->populateUsers();
$this->load->view('admin_cp_view', $data);
}
else
{
session_write_close();
redirect('home', 'refresh');
}
}
我会以不同的方式对模型的populateUsers
方法进行编码。首先,不要盲目使用查询生成器。在某些情况下,这是一个很好的工具。但是你的sql是如此简单,以至于为这样一个超级简单的数据库请求运行大量的查询构建代码是没有意义的。
有关其他更改的"为什么?",请参阅代码注释。
Class User extends CI_Model
{
function populateUsers()
{
$userquery = $this->db->query("Select usernam, accessLevel, fullName from users");
if($userquery->num_rows() > 0)
{
return $userquery->result_array();
}
//{
//else
//you do not need the else because if there were rows the method has returned
//and the next line will never be executed.
//On the other hand, if there are no rows then this line WILL execute
return array(); //Return empty array so that the view won't try to run foreach on a boolean
// }
}
然后在视图中,您的<table>
代码是这样的
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Access Level</th>
<th>Full Name</th>
</tr>
</thead>
<tbody>
<?php
foreach($userList 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>
这会将$userList
中每个项目的一行写入表中。
我想你可能没有正确做出我在第一次尝试回答时建议的更改。您可能有多个页面加载,因为您在foreach
循环中调用了load->view
。现在只有表格行应该重复。
将用户模型更改为:
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();
}
else
{
return false;
}
}
}