如何使用Active Record从codeigniter中的另一个表中获取值?

如何使用Active Record从codeigniter中的另一个表中获取值?

问题描述:

So i have two table and the first one is this, the user_account table.
This table is used for logging in.

ID | login_id | role    | password
---+----------+---------+---------
1  | 1110QA   | admin   | password  
2  | 1110QB   | teacher | password
3  | 1110QC   | teacher | password

And this is my second table which is the teacher table.
And this table is where I will get the information of the teacher like her name.

ID | teacher_id | fname | mname   | lname   | email
---+------------+-------+---------+---------+---------------
1  | 1110QA     | John  | Vincent | Doe     | jvd1@gmail.com
2  | 1110QB     | Mary  | Pop     | Poppins | mpp2@gmail.com
3  | 1110QC     | Julie | Ken     | Clark   | jkc3@gmail.com

What I'm trying to say is I have the userid(login_id) stored in user_account table and the user details stored in the teacher table. So my question is how can I access the values from the teacher table using the account that i logged in? I read about the Active Records but still my code doesn't work. It will not print anything but it doesn't have any error. So here's my code. I'm not sure if my query is correct and my controller.

Controller:

class Site extends CI_Controller {
  public function teacher(){ 
    $this->load->model('model_teacher');
    $id = $this->input->post('idnum'); 
    $data['result'] = $this->model_teacher->scoreboard($id);
    $this->load->view('teacher/teacher', $data);
  }
}

Model:

 class Model_teacher extends CI_Model {
    public function scoreboard($id) {
        $query = $this->db->get_where('teacher', array('login_id' => '$id'));
        return $query->result_array();
    }
} 

View:

<?php
    foreach ($result as $an) {
            echo $an['login_id'];
            echo $an['lname'];
            echo $an['mname'];
            echo $an['fname'];
        }
 ?>    

Please help me and please bear with me guys, I'm still a newbie at using this kind of framework..

You need to use join; CodeIgniter reference (search for join)

class Model_teacher extends CI_Model {
    public function scoreboard( $id ) {

        $q = $this->db->select('user_account.login_id, teacher.*') //select what we need
                      ->join('teacher', 'user_account.login_id = teacher.teacher_id') //do join
                      ->where(['user_account.id' => $id]) //where clause
                      ->get('user_account'); 

        return ($q->num_rows() > 0) ? $q->result_array() : array(); //is there something to return? otherwise return an empty array
    }
}