我有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)在模型中,我能够看到学生表的所有细节,但是当我将值返回给控制器然后查看时我只得到1名学生的详细信息。
2)在这个模型中,除了学生的详细信息,我还希望从student_subject表返回特定学生下的科目,但我不知道如何返回2个值(即学生详细信息和科目详细信息) )从1个模型到1个控制器然后再查看
我关注的代码是
控制器
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'];
}
?>
如果有人能帮我解决问题,我真的很感激
答案 0 :(得分:1)
不要将return
放在循环中。它会立即终止循环。只需在循环外部创建一个临时容器,然后将数据推入其中,最后返回:
基本理念:
public function student_detail($t_id)
{
$data = array(); // initialzie container
$query = $this->db->query("SELECT * FROM teacher where t_id = $t_id");
$results = $query->result_array();
foreach ($results as &$row) {
$s_id = $row['s_id'];
$new_query = $this->db->query("
SELECT s.s_name, ss.subject FROM student s
JOIN student_subject ss ON s.id = ss.s_id
WHERE s.id = $s_id
");
$s = $new_query->result_array();
$row['student_data'] = $s; // push it here
// return $s; // DONNT PUT RETURN HERE!
}
$data = $results;
return $data;
}
或者您可以使用一个查询:
public function student_detail($t_id)
{
return $this->db->query("
SELECT t.t_id, s.s_name, ss.subject
FROM student s
JOIN teacher t ON t.s_id = s.id
JOIN student_subject ss ON s.id = ss.s_id
WHERE t.t_id = {$t_id}
")
->result_array();
}
答案 1 :(得分:1)
修改强>
<强>功能强>
public function dashboard($t_id)
{
$data['student_request'] = $this->student_model->student_detail($t_id);
foreach($data['student_request'] as $row)
{
$s_id = $row['s_id'];
}
$data['final_details'] = $this->student_model->get_students($s_id)
$this->load->view('teacher/dashboard_view',$data);
}
<强> MODEL 强>
public function student_detail($t_id)
{
$this->db->select('*');
$this->db->from('teacher');
$this->db->where('t_id',$t_id);
$query = $this->db->get();
return $query->result_array();
}
public function get_students($s_id)
{
$this->db->select('*');
$this->db->from('student as s');
$this->db->join('student_subject as ss', 's.id = ss.s_id');
$this->db->where('s.id',$s_id);
$query = $this->db->get();
return $query->result_array();
}
查看强>
您现在可以使用
foreach($final_details as $row)
{
echo $row['YOUR_COLUMNS'];
echo $row['s_name'] . ' ' . $row['s_id'] . ' ' . $row['s_subject'];
}
为方便起见,我使用了codeigniters AR。如果你不能那么多,我可以提供更多帮助
修改强>
好的,我已经回顾了自己的答案,发现老师是否有很多学生,只会返回最后一个学生。我会再给你一个选择。
我发现课程教师可以有多个学生,所以这里有一个代码来获取所有这些。
<强>功能强>
public function dashboard($t_id)
{
$data['student_request'] = $this->student_model->student_detail($t_id);
$data['final_details'] = array();
foreach($data['student_request'] as $key => $value)
{
$s_id = $row['s_id'];
$data['final_details'][] = $this->student_model->get_students($s_id)
}
$this->load->view('teacher/dashboard_view',$data);
}
现在你唯一需要知道的是如何遍历那个$ key =&gt; $值。
PHP指南有很多帮助。
答案 2 :(得分:1)
保持控制器代码相同并更改模型和视图
模型
$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; ?>