我有3张桌子
学生
id s_name
1 S1
2 S2
student_subject
id s_id subject
1 1 english
2 1 science
3 2 mathematics
4 2 poetry
老师
id t_id s_id
1 1 1
2 1 2
我正在尝试为老师制作一个仪表板,在那里他可以看到他下面的所有学生以及学生正在关注的科目.
我有老师的id(在t_id中)在控制器中,然后在模型中,从那里我从教师表中获取学生的ID(作为$s_id)并通过这个s_id我希望得到来自student table和student_subject表的详细信息.
我面临的问题是
1) In the model i am able to see all the details of student table but when i return the value to controller and then to view i get the detail of only 1 student.
2) In this model along with the student details i also wish to return the subjects that are under a particular student from student_subject table,however i don’t know how to return 2 values(i.e student details and subject details) from 1 model to 1 controller and then to view
我关注的代码是
调节器
public function dashboard($t_id)
{
$data['student_request'] = $this->student_model->student_detail($t_id);
$this->load->view('teacher/dashboard_view',$data);
}
模型
public function student_detail($t_id)
{
$query = $this->db->query("SELECT * FROM teacher where t_id = $t_id");
foreach ($query->result_array() as $row)
{
$s_id = $row['s_id'];
$new_query = $this->db->query("SELECT * FROM student where id = $s_id");
$s = $new_query->result_array();
//print_r ($s); // just to check the data
return $s;
}
}
视图
PHP
foreach ($student_request as $row)
{
echo $row['s_name'];
}
?>
如果有人能帮我解决这个问题,我真的很感激
最佳答案
保持控制器代码相同并更改模型和视图
模型
$this->db->select('
teacher.s_id,student.s_name,student_subject.subject
');
$this->db->from('teacher');
$this->db->join('student','student.id = teacher.s_id');
$this->db->join('student_subject','student_subject.s_id = teacher.s_id');
$this->db->where('teacher.t_id',$t_id);
$query = $this->db->get();
if($query->num_rows() < 1)
{
return FALSE;
}
return $query->result();
视图
PHP if($student_request): ?>
PHP foreach($student_request as $request): ?>
PHP
echo $request->subject;
echo $request->s_name;
?>
PHP endforeach; ?>
PHP endif; ?>