【发布时间】:2015-01-05 17:51:27
【问题描述】:
我需要从“用户”表中获取名称列表及其 ID,这些表具有我正在显示的组的“组 ID”。我正在使用 CodeIgniter
在我看来,我想完成一个包含以下内容的表格:
| GroupID | Group Name | Users in Group |
-------------------------------------------------------------
| 10001 | Group1 | User1, User3, User5 |
| 10002 | Group2 | User2, User6 |
| 10003 | Group3 | User4 |
所以基本上表中的每个 groupid 条目我都希望在“组中的用户”列下有一个该组中的用户列表,逗号分隔。 “组中的用户”列中的每个用户都是指向其“编辑用户”页面的链接,该页面的 URL 为 mysite.com/admin/edituser/USERID
我的控制器:
class Admin extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->database('admin');
$this->load->model('adminmodel','',TRUE);
}
public function groups() {
if ($this->session->userdata('loggedin')) {
$session_data = $this->session->userdata('loggedin');
$data['username'] = $session_data['username'];
$this->load->view('header', $data);
$this->load->view('sidebar');
$data['group_results'] = $this->adminmodel->list_groups();
foreach ($data['group_results'] as $group_results) {
$data['name_results'][$group_results['id']] = $this->adminmodel->list_users_in_group($group_results['id']);
}
$this->load->view('admin/groups', $data);
$this->load->view('footer-nocharts');
}
else {
//If no session, redirect to login page
redirect('user/login', 'refresh');
}
}
我的模特:
function list_groups() {
$this->db->select('id, name');
$this->db->from('groups')->order_by('id','asc');
$query = $this->db->get();
return $query->result();
}
function list_users_in_group($groupid) {
$this->db->select('users.name, users.id, groups.id');
$this->db->from('users')->order_by('users.name','asc');
$this->db->where('groups.id', $groupid);
$this->db->join('groups', 'groups.id = users.groupid');
$query = $this->db->get();
return $query;
}
还有观点:
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th style="width: 38px;">GroupID</th>
<th style="width: 150px;">Group Name</th>
<th style="width: 40px;">Users Assigned</th>
</tr>
</thead>
<tbody>
<?php foreach ($admin_results as $admin_entry): ?>
<tr class="odd gradeX">
<td><?php echo $admin_entry->id; ?></td>
<td><?php echo $admin_entry->name; ?></td>
<td>
<?php foreach ($name_results as $name): ?>
<?php echo "<a href='/admin/edituser/".$id."'>".$name."</a>, "; ?>
<?php endforeach; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
数据库非常简单,两张表,每列两列。 用户表有列'id'(主键),'name' 组表有列'groupid'(主键),'name'
使用上面代码中的设置,我返回错误: PHP 致命错误:不能在第 36 行的 /var/www/html/application/controllers/admin.php 中使用 stdClass 类型的对象作为数组,我似乎不明白为什么。
感谢您的帮助。
--------------- 编辑 ------ 阅读评论和答案后,我相应地更改了一些内容。
型号:
function list_groups() {
$this->db->select('id, name');
$this->db->from('groups')->order_by('id','asc');
$query = $this->db->get();
return $query->result_array();
}
function list_users_in_group($groupid) {
$this->db->select('users.name, users.id, groups.id');
$this->db->from('users')->order_by('users.name','asc');
$this->db->where('groups.id', $groupid);
$this->db->join('groups', 'groups.id = users.groupid');
$query = $this->db->get();
return $query;
}
(基本上让list_groups返回一个数组)
现在,根据我在该列中的视图,我收到错误:
遇到 PHP 错误
严重性:通知
消息:未定义变量:id
文件名:admin/groups.php
行号:31
遇到 PHP 错误
严重性:4096
消息:CI_DB_mysqli_result 类的对象无法转换为字符串
文件名:admin/groups.php
行号:31
再次感谢。
【问题讨论】:
-
如果你想要一个数组 - 尝试:在模型中的 list_groups() 函数中返回 $query->result_array()。
标签: php mysql codeigniter