如何将数据绑定并输出到codeigniter 3

3lxsmp7m  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(108)

我在数据库中有两个表,一个存储部门列表(id和name),另一个是医生列表,每个医生都附加到科室,而在医生表中,医生通过科室id附加到科室。我的任务是在引导表中显示医生列表,以便科室字段显示的不是科室ID,而是科室名称。我该怎么做呢?

<!-- Table with stripped rows -->
          <table class="table datatable" 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>

控制器

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 class="alert alert-success" role="alert">Congratulation! your account has been created. Please activate your account </div>');
        redirect('admin/doctors_list');
    }

我正在努力这样做

<?=$department_list[$dl['department']]; ?>

但我收到错误

v9tzhpje

v9tzhpje1#

修复后
主计长

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 class="alert alert-success" role="alert">Congratulation! your account has been created. Please activate your account </div>');
        redirect('admin/doctors_list');
    }

}和视图

<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" class="btn btn-success btn-sm" data-bs-toggle="modal" data-bs-target="#editmodal<?= $dl['id']; ?>"><i class="bi bi-pencil-square"></i></a>
                
                 <button type="button" class="btn btn-danger btn-sm" data-bs-toggle="modal" data-bs-target="#delmodal<?=$dl['id']?>">
         <i class="bi bi-trash"></i>
          </button>
                </td>
              </tr>

修复enter image description here之前的站点视图
并且在固定之后
enter image description here

相关问题