【发布时间】:2020-10-07 11:38:06
【问题描述】:
我是 CodeIgniter 4 的新手,我需要显示一个用户列表,其中包含来自多个表的用户详细信息: 用户、users_details、user_contact_details、公司、公司职位、部门。 在表 users_details 中,我有公司、公司职位和部门的外键。
我真的需要你的支持。
这是我的代码。
谢谢!
模型 User_Model.php
namespace App\Models;
use CodeIgniter\Model;
class User_Model extends Model {
public function get_user()
{
return $this->db->table('users')
->join('users_details','users_details.user_id_fk = users.user_id')
->join('user_contact_details','user_contact_details.user_id_fk = users.user_id')
->get()->getResultArray();
}
public function get_user_company_details()
{
return $this->db->table('users_details')
->join('company','company.company_id = users_details.company_id_fk')
->join('company_positions','company_positions.position_id = users_details.position_id_fk')
->join('departments','departments.department_id = users_details.department_id_fk')
->get()->getResultArray();
}
}
控制器用户.php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\User_Model;
class Users extends BaseController
{
protected $User_Model;
public function __construct()
{
$this->User_Model = new User_Model();
}
public function list_users()
{
$data=[
'user' =>$this->User_Model->get_user(),
];
foreach ($data['user'] as $type) {
$type = [
'company' => $this->User_Model->get_user_company_details(),
];
}
echo view('users/list_users', $data);
}
}
查看list_users.php
<table id="useful_links" class="table table-striped projects" role="grid" aria-describedby="example2_info">
<thead>
<tr>
<td>Id</td>
<td>Name</td>
<td>Phone</td>
<td>E-mail</td>
<td>Vacation days</td>
<td>Position</td>
<td>Active</td>
<td>Deleted</td>
<td>Options</td>
</tr>
</thead>
<?php
$n=1;
foreach ($user as $row)
{?>
<tr>
<td><?php echo $n;?>
<td><div class="user-block">
<span class="username" style="margin-top:5px;"><?php echo $row['surname']." ".$row['firstname'];?> </span></div></td>
<td><?php echo $row['user_phone'];?></td>
<td><?php echo $row['user_email'];?></td>
<td><?php echo $row['yearly_vacation_days'];?></td>
<td>
<?php foreach ($company as $row)
{
echo $row['position_name'];
}?>
</td>
<td><?php echo $row['user_active'];?></td>
<td><?php echo $row['user_deleted'];?></td>
<td>
<a href="<?php echo site_url('/useful_links/editUseful_link/'.$row['link_id']);?>" class="btn btn-primary btn-sm" href="#"><i class="fas fa-folder"></i> View</a>
<a href="<?php echo site_url('/useful_links/editUseful_link/'.$row['link_id']);?>" class="btn btn-info btn-sm"><i class="fas fa-pencil-alt"></i> Edit</a>
<a href="<?php echo site_url('/useful_links/delete/'.$row['link_id']);?>" class="btn btn-danger btn-sm"><i class="fas fa-trash"></i> Delete</a>
</td>
<?php $n++; }
?>
</table>
【问题讨论】:
-
您希望输出的准确程度如何?只有一个数组,还是一个数组只包含信息,键包含其余信息?
-
像一个数组一样输出,其中仅包含信息,而键包含其余信息。谢谢
标签: php codeigniter-4