I have two tables in the database, one stores a list of departments (id and name), and the other a list of doctors, and each doctor is attached to the department, while in the table of doctors, the doctor is attached to the department by department id. My task is to display a list of doctors in a bootstrap table and so that the department field displays not the department ID but the name of the department, how can I do it?
view
<!-- Table with stripped rows -->
<table id="example">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Department</th>
<th scope="col">Phone</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
<?php foreach ($doctors_list as $dl) : ?>
<tr>
<th scope="row"><?= $i; ?></th>
<td><?= $dl['name']; ?></td>
<td><?= $dl['email']; ?></td>
<td><?=$dl['department']; ?> </td>
<td><?= $dl['phone']; ?></td>
</tr>
<?php $i ; ?>
<?php endforeach; ?>
</tbody>
</table>
controller
public function doctors_list()
{
$data['title'] = 'Dashboard';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
$data['doctors_list'] = $this->db->get_where('user', ['role_id' => 2])->result_array();
$data['department_list'] = $this->db->get('departments')->result_array();
$this->form_validation->set_rules('name', 'Name', 'required|trim');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]', [
'is_unique' => 'This email has already registered!'
]);
$this->form_validation->set_rules('password1', 'Password', 'required|trim|min_length[3]|matches[password2]', [
'matches' => 'Password dont match!',
'min_length' => 'Password too short!'
]);
$this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
if ($this->form_validation->run() == false) {
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar_admin', $data);
$this->load->view('admin/doctors_list', $data);
$this->load->view('templates/footer');
} else {
$email = $this->input->post('email', true);
$role_id = $this->input->post('role_id', true);
$department = $this->input->post('department', true);
$data = [
'name' => htmlspecialchars($this->input->post('name', true)),
'email' => htmlspecialchars($email),
'image' => 'default.jpg',
'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
'role_id' => htmlspecialchars($role_id),
'department' => htmlspecialchars($department),
'phone' => htmlspecialchars($this->input->post('phone', true)),
'is_active' => 1,
'date_created' => time()
];
$this->db->insert('user', $data);
$this->session->set_flashdata('message', '<div role="alert">Congratulation! your account has been created. Please activate your account </div>');
redirect('admin/doctors_list');
}
i am trying to do so
<?=$department_list[$dl['department']]; ?>
but i get an error
CodePudding user response:
I'm guessing that's giving you an error because the keys of the array returned by the database are not the department ids. The department ids are one level deeper inside that array, so you're comparing to the wrong value.
To fix, either create an array that has the department id as the key and the department name as the value, like this:
$departments = $this->db->get('departments')->result_array();
$data['department_list'] = array_combine(
array_column($departments, 'id'),
array_column($departments, 'name')
);
The loop in the view then looks like this:
<?php foreach ($doctors_list as $dl) : ?>
<tr>
<th scope="row"><?= $i; ?></th>
<td><?= $dl['name']; ?></td>
<td><?= $dl['email']; ?></td>
<td><?= $department_list[$dl['department']]; ?></td>
<td><?= $dl['phone']; ?></td>
</tr>
<?php $i ; ?>
<?php endforeach; ?>
Or join the departments table to the user table and get all of the data in one query:
$this->db->select('u.*, d.name AS department');
$this->db->from('user u');
$this->db->join('departments d', 'd.id = u.department');
$this->db->where('u.role_id', 2);
$data['doctors_list'] = $this->db->get()->result_array();
You can then remove the query that gets the departments from the controller.
And the loop in the view stays the same as in your opening post:
<?php foreach ($doctors_list as $dl) : ?>
<tr>
<th scope="row"><?= $i; ?></th>
<td><?= $dl['name']; ?></td>
<td><?= $dl['email']; ?></td>
<td><?= $dl['department']; ?></td>
<td><?= $dl['phone']; ?></td>
</tr>
<?php $i ; ?>
<?php endforeach; ?>
(Haven't tested this, so there may be some syntax errors.)
CodePudding user response:
After the fix
Controller
public function doctors_list()
{
$data['title'] = 'Dashboard';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
$data['doctors_list'] = $this->db->get_where('user', ['role_id' => 2])->result_array();
$departments = $this->db->get('departments')->result_array();
$data['department_list'] = array_combine(array_column($departments, 'id'),array_column($departments, 'dep_name') );
$this->form_validation->set_rules('name', 'Name', 'required|trim');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]', [
'is_unique' => 'This email has already registered!'
]);
$this->form_validation->set_rules('password1', 'Password', 'required|trim|min_length[3]|matches[password2]', [
'matches' => 'Password dont match!',
'min_length' => 'Password too short!'
]);
$this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
if ($this->form_validation->run() == false) {
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar_admin', $data);
$this->load->view('admin/doctors_list', $data);
$this->load->view('templates/footer');
} else {
$email = $this->input->post('email', true);
$role_id = $this->input->post('role_id', true);
$department = $this->input->post('department', true);
$data = [
'name' => htmlspecialchars($this->input->post('name', true)),
'email' => htmlspecialchars($email),
'image' => 'default.jpg',
'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
'role_id' => htmlspecialchars($role_id),
'department' => htmlspecialchars($department),
'phone' => htmlspecialchars($this->input->post('phone', true)),
'is_active' => 1,
'date_created' => time()
];
$this->db->insert('user', $data);
$this->session->set_flashdata('message', '<div role="alert">Congratulation! your account has been created. Please activate your account </div>');
redirect('admin/doctors_list');
}
} And View
<tr>
<th scope="row"><?= $i; ?></th>
<td><?= $dl['name']; ?></td>
<td><?= $dl['email']; ?></td>
<td><?= $department_list[$dl['department']]; ?> </td>
<td><?= $dl['phone']; ?></td>
<td>
<a type="button" data-bs-toggle="modal" data-bs-target="#editmodal<?= $dl['id']; ?>"><i ></i></a>
<button type="button" data-bs-toggle="modal" data-bs-target="#delmodal<?=$dl['id']?>">
<i ></i>
</button>
</td>
</tr>
View of the site before fixing enter image description here
and after fix