php - 无法在 codeigniter 中从模型到 Controller 获取整个数据值

标签 php mysql codeigniter

我有3张 table

学生

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

我正在尝试为老师制作一个仪表板,他可以在其中看到他手下的所有学生以及学生正在学习的科目。
我有在 Controller 中携带的老师的 ID(如 t_id ),然后在模型中,从那里我从教师表中获取学生的 ID(如 $s_id ),并通过这个 s_id 我希望得到来自 student 表和 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



我正在关注的代码是

Controller
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'];
    }
?>

如果有人能帮我解决这个问题,我将不胜感激

最佳答案

编辑

功能

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);
}

型号
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 => $value。

PHP 指南有很大帮助。

关于php - 无法在 codeigniter 中从模型到 Controller 获取整个数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39137528/

相关文章:

php - CodeIgniter Localhost 重定向到 xampp 主屏幕

javascript - PHP 或 Javascript/Jquery - 计算新生儿/成人的年龄

javascript - javascript 数组中有新行时出错

php - javascript/jquery : get info for a remote image

php mysqli 受影响的行在删除语句后返回错误结果

php - 执行存储在数据库中的 PHP 代码

php - 带有 CLI 脚本的 Codeigniter 站点

php - 如何解决 "Fatal error: Class ' MySQLi'未找到”?

php - LIKE 表达式中的 MySQL 连字符

php - 我想获得两个数组之间的差异