I have 2 database tables 'super_admin_staff' and 'super_admin_roles'
The staff table is made up like :
id
name
email
phone
role
The role table is made up like :
id
role
The id column from the roles table is unique and is associated with the 'roles' column of the staff table.
At the moment i am fetching all the roles from the database using model :
function superAdminStaffRoles()
{
$this->db->select('*');
$this->db->from('super_admin_roles');
$query = $this->db->get();
$result = $query->result();
return $result;
}
In my controller i pass it over to my view like this :
public function superAdminStaffRoles()
{
if ($this->isSuperAdmin() != true) {
$this->loadThis();
} else {
$this->load->model('super_admin_staff_model');
$data['userRecords'] = $this->super_admin_staff_model->superAdminStaffRoles();
$this->global['pageTitle'] = 'Staff Roles';
$this->global['pageDesc'] = 'Add or Edit new Staff Roles and Permissions';
$this->loadViews("super_admin/staff_roles", $this->global, $data, null);
}
}
then display the data in my view like :
<tbody>
<?php
if (!empty($userRecords)) {
foreach ($userRecords as $record) {
?>
<tr>
<td><?php echo $record->role ?></td>
<td>where want to loop through the association</td>
<td class="text-center">
<a class="btn btn-sm btn-info" href="<?php echo base_url() ?>/EditRole/<?php echo $record->id; ?>"><i class="fas fa-edit"></i></a>
<a class="btn btn-sm btn-danger deleteUser" href="<?php echo base_url() ?>/DeleteRole/<?php echo $record->id; ?>"><i class="fas fa-trash"></i></a>
</td>
</tr>
<?php
}
}
?>
</tbody>
How can i fetch the additional information i need in my model to display how many staff are associated with that role? i think i may need to join the tables and i don't have much knowledge on joins.
can it be done in the same query?
the result should be something like :
Staff table
|id | name | role |
.......................
|1 | staff1 | 1 |
|2 | staff2 | 1 |
|3 | staff3 | 2 |
|4 | staff4 | 3 |
|5 | staff5 | 3 |
Role Table
|id | role |
...............
|1 | role 1 |
|2 | role 2 |
|3 | role 3 |
and the result should look like :
|Staff Role | Assigned |
........................
| Role1 | 2 |
| Role2 | 1 |
| Role3 | 2 |
Any help appreciated
Here you go
First, you need to join the two tables
INNER JOIN
$this->db->from('super_admin_staff');
$this->db->join('super_admin_roles', 'super_admin_staff.role = super_admin_roles.id');
You'll get the following
|id | name | role |id | role |
......................................
|1 | staff1 | 1 |1 | role 1 |
|2 | staff2 | 1 |1 | role 1 |
|3 | staff3 | 2 |2 | role 2 |
|4 | staff4 | 3 |3 | role 3 |
|5 | staff5 | 3 |3 | role 3 |
LEFT JOIN
$this->db->from('super_admin_staff');
$this->db->join('super_admin_roles', 'super_admin_staff.role = super_admin_roles.id', 'left');
You'll get the following
|id | name | role |id | role |
......................................
|1 | staff1 | 1 |1 | role 1 |
|2 | staff2 | 1 |1 | role 1 |
|3 | staff3 | 2 |2 | role 2 |
|4 | staff4 | 3 |3 | role 3 |
|5 | staff5 | 3 |3 | role 3 |
|6 | staff6 | 0 |null| null |
GROUP BY
Then grouping (which is strongly associated with the select because what is in the select clause MUST be in the group by). Here you want to group by the role, you can do by grouping with super_admin_staff.role, super_admin_roles.id or super_admin_roles.role. As you want the super_admin_roles.role in the output, let's take it as grouping column.
Using COUNT(*) will return the number of rows for each group.
$this->db->select('super_admin_roles.role as StaffRole, COUNT(*) as Assigned');
$this->db->from('super_admin_staff');
$this->db->join('super_admin_roles', 'super_admin_staff.role = super_admin_roles.id');
$this->db->group_by('super_admin_roles.role');
$query = $this->db->get();