Sam Sam - 4 months ago 12
MySQL Question

Not able to fetch the entire data values from model to controller in codeigniter

I have 3 tables


id s_name
1 S1
2 S2


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

I am trying to make a dashboard for a teacher where he can see all the students under him and the subjects that a student is following.
I have the id of the teacher which is being carried (as
) in the controller and then in the model, from there i am getting the id of the student (as
) from teacher table and through this
i wish to get the details from student table and student_subject table.

The issues that i am facing is

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

Code that i am following is


public function dashboard($t_id)
$data['student_request'] = $this->student_model->student_detail($t_id);


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;


foreach ($student_request as $row)
echo $row['s_name'];

Would really appreciate if anyone could help me with the problem

Sam Sam

Keep the controller code same and change the model and view



$this->db->join('student',' = teacher.s_id');
$this->db->join('student_subject','student_subject.s_id = teacher.s_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): ?>

    echo $request->subject;
    echo $request->s_name;  


    <?php  endforeach; ?>

    <?php endif; ?>